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

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

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

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>yjmyzz</groupId>
<artifactId>jsf-web</artifactId>
<version>1.0</version>
<packaging>war</packaging> <dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency> <!-- jsf -->
<dependency>
<groupId>org.jboss.spec.javax.faces</groupId>
<artifactId>jboss-jsf-api_2.1_spec</artifactId>
<version>2.1.19.1.Final-redhat-1</version>
<scope>compile</scope>
</dependency> <!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.2.RELEASE</version>
</dependency> <!-- servlet支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>webapp</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>

pom.xml

1. 自动加载配置文件

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

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>jsf-web</display-name> <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/applicationContext-*.xml
</param-value>
</context-param> </web-app>

web.xml

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

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

 package yjmyzz.utils;

 import javax.faces.context.FacesContext;
import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; public class ApplicationContextUtils { public static ApplicationContext getApplicationContext() {
ServletContext context = (ServletContext) FacesContext
.getCurrentInstance().getExternalContext().getContext();
ApplicationContext appctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(context); return appctx;
} public static <T> T getBean(Class<T> t) {
return getApplicationContext().getBean(t);
}
}

ApplicationContextUtils

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

3. 使用properties文件注入

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

 package yjmyzz.entity;

 import java.io.Serializable;

 public class ProductEntity implements Serializable {

     private static final long serialVersionUID = -2055674628624266800L;
/*
* 产品编码
*/
private String productNo; /**
* 产品名称
*/
private String productName; /**
* 产品ID
*/
private Long productId; public String getProductNo() {
return productNo;
} public void setProductNo(String productNo) {
this.productNo = productNo;
} public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
} public Long getProductId() {
return productId;
} public void setProductId(Long productIdLong) {
this.productId = productIdLong;
} @Override
public String toString() {
return productId + "/" + productNo + "/" + productName;
} }

ProductEntity

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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/*.properties</value>
</list>
</property>
</bean> <bean id="productEntity" class="yjmyzz.entity.ProductEntity">
<property name="productId" value="${product.id}" />
<property name="productNo" value="${product.no}" />
<property name="productName" value="${product.name}" />
<!-- <property name="productId">
<bean class="java.lang.Long">
<constructor-arg index="0" value="${product.id}" />
</bean>
</property>
-->
</bean>
</beans>

spring配置文件

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

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

product.properties

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

4.验证注入是否成功

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

 package yjmyzz.controller;

 import javax.faces.bean.ManagedBean;

 import yjmyzz.entity.ProductEntity;
import yjmyzz.utils.ApplicationContextUtils; @ManagedBean(name = "Home")
public class HomeController { public String sayHello() { ProductEntity product = ApplicationContextUtils
.getBean(ProductEntity.class); return product.toString();
} }

HomeController

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

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head>
<title>jsf-web</title>
</h:head>
<body>
<h1>
#{Home.sayHello()} </h1>
</body>
</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. 持续集成(CI)初探

    前不久接触了持续集成(Continuous Integration,CI). 一.持续集成是什么 首先说说“集成”的概念.在实际的软件开发中,常常会发生两种情境: 1.几个项目组对同一个系统的不同功能 ...

  2. ORA-00988: missing or invalid password(s)

    创建账号或修改账号密码时有可能会遇到ORA-00988: missing or invalid password(s),那么什么情况下会遇到这种错误呢? 一般是因为密码的设置不符合命名规范: 1:密码 ...

  3. spring Quartz 调度

    Quartz 是开源任务调度框架中的翘首,它提供了强大任务调度机制,同时保持了使用的简单性.Quartz 允许开发人员灵活地定义触发器的调度时间表,并可以对触发器和任务进行关联映射.此外,Quartz ...

  4. Join的表顺序

    在今天的文章里,我想谈下SQL Server里一个非常有趣的话题:在表联接里,把表指定顺序的话是否有意义?每次我进行查询和性能调优的展示时,大家都会问我他们是否应该把联接中的表指定下顺序,是否会帮助查 ...

  5. mybatis oracle BLOB类型字段保存与读取

    一.BLOB字段 BLOB是指二进制大对象也就是英文Binary Large Object的所写,而CLOB是指大字符对象也就是英文Character Large Object的所写.其中BLOB是用 ...

  6. 烂泥:rsync与inotify集成实现数据实时同步更新

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上篇文章我们介绍了如何使用rsync同步文件,这篇文章我们再来介绍下,如何把rsync与inotify集成实现数据的实时同步. 要达到这个目的,我们需要 ...

  7. 快速操作Linux终端命令行的快捷键列表

    终端有很多快捷键,不太好记,常用的在这里 Ctrl+r 实现快速检索使用过的历史命令.Ctrl+r中r是retrieve中r.Ctrl+a:光标回到命令行首. (a:ahead)Ctrl+e:光标回到 ...

  8. Android程序函数 将assets文件夹下的文件复制到手机的sd卡中(包括子文件夹)

    最近在做个功能是将asset文件夹下的所有文件(包括子文件)全部拷贝出来到指定目录下.所用的方法无非是用AssetManager.但是这里 有个问题是也要讲子文件夹和子文件都要拷贝出来.到网上Goog ...

  9. Android 横竖屏切换小结

    (自己体会:每次横竖屏自动切时都会run Activity的onCreate,即相当后重新进入Activity初始化一样:) 转自:http://www.cnblogs.com/franksunny/ ...

  10. raspberry pi2 智能小车源码及测试视频

    作者:XIAOBO QQ:463431476 转载请注明作者Python 源代码 import RPi.GPIO as GPIO  #human-computer-interaction import ...