在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构

src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到WEB-INF下

首先看下pom.xml,需要引入一些依赖项:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>yjmyzz</groupId>
 5     <artifactId>jsf-web</artifactId>
 6     <version>1.0</version>
 7     <packaging>war</packaging>
 8
 9
10     <dependencies>
11         <!-- 单元测试 -->
12         <dependency>
13             <groupId>junit</groupId>
14             <artifactId>junit</artifactId>
15             <version>4.7</version>
16             <scope>test</scope>
17         </dependency>
18
19         <!-- jsf -->
20         <dependency>
21             <groupId>org.jboss.spec.javax.faces</groupId>
22             <artifactId>jboss-jsf-api_2.1_spec</artifactId>
23             <version>2.1.19.1.Final-redhat-1</version>
24             <scope>compile</scope>
25         </dependency>
26
27
28         <!-- spring -->
29         <dependency>
30             <groupId>org.springframework</groupId>
31             <artifactId>spring-context</artifactId>
32             <version>4.0.2.RELEASE</version>
33         </dependency>
34
35
36         <dependency>
37             <groupId>org.springframework</groupId>
38             <artifactId>spring-web</artifactId>
39             <version>4.0.2.RELEASE</version>
40         </dependency>
41
42
43         <!-- servlet支持 -->
44         <dependency>
45             <groupId>javax.servlet</groupId>
46             <artifactId>servlet-api</artifactId>
47             <version>2.5</version>
48         </dependency>
49
50     </dependencies>
51
52     <build>
53         <plugins>
54             <plugin>
55                 <artifactId>maven-compiler-plugin</artifactId>
56                 <version>3.1</version>
57                 <configuration>
58                     <source>1.7</source>
59                     <target>1.7</target>
60                 </configuration>
61             </plugin>
62             <plugin>
63                 <artifactId>maven-war-plugin</artifactId>
64                 <version>2.3</version>
65                 <configuration>
66                     <warSourceDirectory>webapp</warSourceDirectory>
67                     <failOnMissingWebXml>false</failOnMissingWebXml>
68                 </configuration>
69             </plugin>
70         </plugins>
71     </build>
72 </project>

pom.xml

1. 自动加载配置文件

在web项目中,可以让spring自动加载配置文件(即上图中的src/main/resouces/spring下的xml文件),WEB-INF/web.xml中参考以下设置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3      <display-name>jsf-web</display-name>
 4
 5      <welcome-file-list>
 6        <welcome-file>index.html</welcome-file>
 7      </welcome-file-list>
 8
 9     <listener>
10      <listener-class>
11        org.springframework.web.context.ContextLoaderListener
12       </listener-class>
13     </listener>
14
15     <context-param>
16      <param-name>contextConfigLocation</param-name>
17       <param-value>
18        classpath*:spring/applicationContext-*.xml
19       </param-value>
20     </context-param>
21
22 </web-app>

web.xml

解释一下: classpath*:spring/applicationContext-*.xml 这里表示将加载classpath路径下 spring目录下的所有以applicationContext-开头的xml文件 , 通常为了保持配置文件的清爽 , 我们会把配置分成多份 : 比如 applicationContext-db.xml 用来配置DataSource , applicationContext-cache.xml用来配置缓存...等等.

2.代码中如何取得ApplicationContext实例

 1 package yjmyzz.utils;
 2
 3 import javax.faces.context.FacesContext;
 4 import javax.servlet.ServletContext;
 5
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.web.context.support.WebApplicationContextUtils;
 8
 9 public class ApplicationContextUtils {
10
11     public static ApplicationContext getApplicationContext() {
12         ServletContext context = (ServletContext) FacesContext
13                 .getCurrentInstance().getExternalContext().getContext();
14         ApplicationContext appctx = WebApplicationContextUtils
15                 .getRequiredWebApplicationContext(context);
16
17         return appctx;
18     }
19
20     public static <T> T getBean(Class<T> t) {
21         return getApplicationContext().getBean(t);
22     }
23 }

ApplicationContextUtils

有了这个工具类 , 就可以方便的取得注入的Bean

3. 使用properties文件注入

为了演示注入效果,先定义一个基本的Entity类

 1 package yjmyzz.entity;
 2
 3 import java.io.Serializable;
 4
 5 public class ProductEntity implements Serializable {
 6
 7     private static final long serialVersionUID = -2055674628624266800L;
 8     /*
 9      * 产品编码
10      */
11     private String productNo;
12
13     /**
14      * 产品名称
15      */
16     private String productName;
17
18     /**
19      * 产品ID
20      */
21     private Long productId;
22
23     public String getProductNo() {
24         return productNo;
25     }
26
27     public void setProductNo(String productNo) {
28         this.productNo = productNo;
29     }
30
31     public String getProductName() {
32         return productName;
33     }
34
35     public void setProductName(String productName) {
36         this.productName = productName;
37     }
38
39     public Long getProductId() {
40         return productId;
41     }
42
43     public void setProductId(Long productIdLong) {
44         this.productId = productIdLong;
45     }
46
47     @Override
48     public String toString() {
49         return productId + "/" + productNo + "/" + productName;
50     }
51
52 }

ProductEntity

然后在applicationContext-beans.xml中配置以下内容:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 6
 7     <bean id="propertyConfigurer"
 8         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 9         <property name="locations">
10             <list>
11                 <value>classpath:properties/*.properties</value>
12             </list>
13         </property>
14     </bean>
15
16     <bean id="productEntity" class="yjmyzz.entity.ProductEntity">
17         <property name="productId" value="${product.id}" />
18         <property name="productNo" value="${product.no}" />
19         <property name="productName" value="${product.name}" />
20         <!-- <property name="productId">
21             <bean class="java.lang.Long">
22                 <constructor-arg index="0" value="${product.id}" />
23             </bean>
24         </property>
25          -->
26     </bean>
27 </beans>

spring配置文件

注:classpath:properties/*.properties表示运行时 , spring容器会自动加载classpath\properties目录下的所有以.properties后缀结尾的文件 ,  我们在src/main/resources/properties/下放置一个product.properties属性文件 , 内容如下:

1 product.id=3
2 product.no=n95
3 product.name=phone

product.properties

该文件被spring自动加载后 , 就可以用里面定义的属性值 , 为Bean做setter属性注入 , 即配置文件中的<property name="productId" value="${product.id}" />

4.验证注入是否成功

在HomeController里 ,  向Spring容器要一个Bean ,  显示下它的属性:

 1 package yjmyzz.controller;
 2
 3 import javax.faces.bean.ManagedBean;
 4
 5 import yjmyzz.entity.ProductEntity;
 6 import yjmyzz.utils.ApplicationContextUtils;
 7
 8 @ManagedBean(name = "Home")
 9 public class HomeController {
10
11     public String sayHello() {
12
13         ProductEntity product = ApplicationContextUtils
14                 .getBean(ProductEntity.class);
15
16         return product.toString();
17     }
18
19 }

HomeController

index.xhtml里仍然跟上篇相同:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml"
 3       xmlns:h="http://java.sun.com/jsf/html"
 4       xmlns:f="http://java.sun.com/jsf/core"
 5       xmlns:ui="http://java.sun.com/jsf/facelets">
 6
 7 <h:head>
 8     <title>jsf-web</title>
 9 </h:head>
10 <body>
11     <h1>
12         #{Home.sayHello()}
13
14     </h1>
15 </body>
16 </html>

index.xhtml

最后部署到jboss上 , 运行截图如下:

转载于:https://www.cnblogs.com/yjmyzz/p/3604007.html

spring-自动加载配置文件\使用属性文件注入相关推荐

  1. tmux不自动加载配置文件.tmux.conf

    /*********************************************************************** tmux不自动加载配置文件.tmux.conf* 说明 ...

  2. Spring Boot加载配置文件

    问题1:Spring如何加载配置,配置文件位置? 1.默认位置: Spring Boot默认的配置文件名称为application.properties,SpringApplication将从以下位置 ...

  3. Tomcat 7 自动加载类及检测文件变动原理

    在一般的 web 应用开发里通常会使用开发工具(如 Eclipse.IntelJ )集成 tomcat ,这样可以将 web 工程项目直接发布到 tomcat 中,然后一键启动.经常遇到的一种情况是直 ...

  4. 关于spring自动加载的那点事儿

    背景 惯例要讲一下背景,毕竟问题来源于生活,困难滋生于工作,要是每天吃吃喝喝.无忧无虑,我相信我也没什么问题好写了^_^公司架构组在推新的基础框架,主要是嫌以前的框架用起来太啰嗦了,做了很多感觉多余的 ...

  5. python自动加载配置文件中模块名_python----读取配置文件(configparser模块)

    一.configparser模块 在工作中,常常需要把小脚本共享给其他人用.他人在使用的时候,查看修改源码不太方便.于是想到使用python中的configparser模块,只需要修改配置文件就可以运 ...

  6. PHP PSR4自动加载代码赏析

    转载地址:https://www.cnblogs.com/wangmy/p/6692970.html 第一部分是引入自动加载配置文件 1.入口文件:autoload.php 里面没什么东西,就是导入C ...

  7. Spring Boot加载指定属性文件

    我们可以通过@PropertySource注解来加载指定的属性文件,可以将配置文件内的属性映射到我们的controller.service和实体类里面去. 下面来介绍配置过程: 1.添加依赖: < ...

  8. php入门篇-------PHPCMS 入口文件,自动加载系统函数和URL规则

    这里主要分析PHPCMS的入口文件和系统自动加载配置文件和系统函数,还有URL的规则: 首先是入口文件分析: index.php 2,框架主文件分析:(这里主要分析加载配置文件和公共函数,还有URL规 ...

  9. thinkphp 框架自动加载原理_这下你应该理解ThinkPHP的Loader自动加载了

    想了很久终于要开始系列文章的编写了,期望是写出提升和面试都可以搞定的系列文章. 当你看到本文时,如果你发现咔咔没有编写到的面试热点问题或者技术难点,期待评论区指出,一起完善. 前言 目前再整理PHP进 ...

最新文章

  1. matplotlib 和 pandas 两个包的安装
  2. python读取邮件发送日期和时间_Python读取指定日期邮件的实例
  3. Java键盘字符乱码判断代码
  4. c++编辑器_盘点四款PDF编辑器,使用它们,编辑PDF文件没问题!
  5. ubuntu14.04管理员密码忘记的解决方法
  6. JS处理Cookie
  7. Amazon软件开发工程师面试题
  8. [转载] 中华典故故事(孙刚)——35 一文钱难倒英雄汉
  9. 硬货专栏 |深入浅出 WebRTC AEC(声学回声消除)
  10. response setHeader 设置下载中文文件名乱码问题
  11. 多益网络2015校园招聘第二次笔试题
  12. RMVB格式介绍,如何播放该格式视频,以及将RMVB转换成MP4?
  13. 备份和恢复 ESXi 主机配置
  14. 机电信息杂志机电信息杂志社机电信息编辑部2022年第13期目录
  15. 人大金仓数据库的使用心得
  16. 【SpringBoot】3、SpringBoot中整合Thymeleaf模板引擎
  17. 华纳云:盘点那些年操作系统的成长史
  18. 为什么要建立一个行之有效的医疗保健初创企业几乎是不可能的
  19. SH103A型全自动微量水分测定仪
  20. 英语读书笔记-Book Lovers Day 07

热门文章

  1. Linux 文件系统初探
  2. 51NOD 1212 无向图最小生成树
  3. linux下的各种shell介绍(bash和dash转换)
  4. 千千万万的IT开发工程师路在何方?
  5. asp.net MVC中的tip
  6. dask 使用_在Google Cloud上使用Dask进行可扩展的机器学习
  7. 美国人口普查年收入比赛_训练网络对收入进行分类:成人普查收入数据集
  8. 全志A33-linux内核early_printk分析及使用
  9. 华为手机销量超过苹果,华为能算是全球第二大手机厂家吗?
  10. ios 检测是否联网_秋招|阿里 iOS 五轮面经分享,已收到阿里的意向书