上一篇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. <dependencies>
  10. <!-- 单元测试 -->
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>4.7</version>
  15. <scope>test</scope>
  16. </dependency>
  17.  
  18. <!-- jsf -->
  19. <dependency>
  20. <groupId>org.jboss.spec.javax.faces</groupId>
  21. <artifactId>jboss-jsf-api_2.1_spec</artifactId>
  22. <version>2.1.19.1.Final-redhat-1</version>
  23. <scope>compile</scope>
  24. </dependency>
  25.  
  26. <!-- spring -->
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-context</artifactId>
  30. <version>4.0.2.RELEASE</version>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-web</artifactId>
  36. <version>4.0.2.RELEASE</version>
  37. </dependency>
  38.  
  39. <!-- servlet支持 -->
  40. <dependency>
  41. <groupId>javax.servlet</groupId>
  42. <artifactId>servlet-api</artifactId>
  43. <version>2.5</version>
  44. </dependency>
  45.  
  46. </dependencies>
  47.  
  48. <build>
  49. <plugins>
  50. <plugin>
  51. <artifactId>maven-compiler-plugin</artifactId>
  52. <version>3.1</version>
  53. <configuration>
  54. <source>1.7</source>
  55. <target>1.7</target>
  56. </configuration>
  57. </plugin>
  58. <plugin>
  59. <artifactId>maven-war-plugin</artifactId>
  60. <version>2.3</version>
  61. <configuration>
  62. <warSourceDirectory>webapp</warSourceDirectory>
  63. <failOnMissingWebXml>false</failOnMissingWebXml>
  64. </configuration>
  65. </plugin>
  66. </plugins>
  67. </build>
  68. </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上 , 运行截图如下:

spring-自动加载配置文件\使用属性文件注入的更多相关文章

  1. springboot属性类自动加载配置文件中的值

    springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...

  2. 关于不重启Tomcat自动加载改变的class文件

    修改server.xml,在Host标签下加入以下配置 <Context path="" docBase="FileManager" reloadable ...

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

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

  4. Nginx自动加载配置文件方案

    nginx自动加载配置文件方案一.nginx+consul+consul-template实现过程:consul作为服务发现软件,consul-template作为nginx配置文件的模板,consu ...

  5. Spring Boot加载配置文件

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

  6. Tomcat7 自动加载类及检测文件变动原理

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

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

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

  8. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  9. spring boot加载配置文件的顺序

    四个默认加载配置文件地方的优先级,四个文件相同配置有优先级概念  不同位置相互补充 外部配置文件不建议使用,不符合maven项目结构,打包会打不进去

随机推荐

  1. CSRF攻击与防御

    CSRF是什么 CSRF在百度百科中是这么说的:“CSRF(Cross-site request forgery跨站请求伪造,也被称为“one click attack”或者session ridin ...

  2. Java Security:keytool工具使用说明

    Keytool用法说明 Keytool是一个key与cert的管理工具.使用keytool可以管理public key.private key,以及与key之相关的certificate. 1.com ...

  3. 十五天精通WCF——第九天 高级玩法之自定义Behavior

    终于我又看完了二期爱情保卫战,太酸爽了,推荐链接:http://www.iqiyi.com/a_19rrgublqh.html?vfm=2008_aldbd,不多说,谁看谁入迷,下面言归正传, 看看这 ...

  4. Use Excel Pivot Table as a BI tool

    Normally, we have created a table, view in database or cube in SSAS, user can use Excel as a BI tool ...

  5. 字典dictionary

    字典,即我们可以通过索引来查找更详细的内容.每个item都是由一对index:value构成的 索引可以有副本,但是试图根据存在副本的索引访问元素时,只会取最靠后的那个. 索引必须是immutable ...

  6. 四、Android学习第四天——JAVA基础回顾(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 四.Android学习第四天——JAVA基础回顾 这才学习Android的 ...

  7. Can't load AMD 64-bit .dll on a IA 32-bit platform

    主要谈谈在win8.1(64bit)下搭建环境的经历. 安装win8.1(64bit)后,配置java环境是费了我一番心思的,所以想记录下来,成为经验.64位系统下比较理想的配置应该是 64位jdk ...

  8. QT5.5.0版本添加icon图标步骤

    1.制作icon图标文件 可以进入这个网站在线制作:http://www.ico.la/ 2.创建资源文件:qrc文件 接着 添加2两项,先点击prefix,然后添加文件--->图标路径 3.可 ...

  9. STM32F407 RCC时钟配置

    新上手项目需要使用STM32F407,在使用STM32F1系列时就喜欢自己用库函数设置系统时钟,所以F4也打算这么做,但是遇到了一些问题. 其中百度文库有篇文章关于RCC的文章将的不错,地址:http ...

  10. 二:shell之bash变量

    1.变量的分类: 用户自定义变量:   变量自定义 默认存储是字符串环境变量:              这种变量中主要保存的是和系统操作环境相关的数据.变量可以自定义,但是对系统生效的环境变量名和变 ...