java 注入配置文件_spring-自动加载配置文件\使用属性文件注入

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

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

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC1  3     4.0.0 4     yjmyzz 5     jsf-web 6     1.0 7     war 8  9 10     11         12         13             junit14             junit15             4.716             test17         18 19         20         21             org.jboss.spec.javax.faces22             jboss-jsf-api_2.1_spec23             2.1.19.1.Final-redhat-124             compile25         26 27 28         29         30             org.springframework31             spring-context32             4.0.2.RELEASE33         34 35 36         37             org.springframework38             spring-web39             4.0.2.RELEASE40         41 42 43         44         45             javax.servlet46             servlet-api47             2.548         49 50     51 52     53         54             55                 maven-compiler-plugin56                 3.157                 58                     1.759                     1.760                 61             62             63                 maven-war-plugin64                 2.365                 66                     webapp67                     false68                 69             70         71     72 

pom.xml

1. 自动加载配置文件

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC1 <?xml  version="1.0" encoding="UTF-8"?> 2  3      jsf-web 4       5       6        index.html    7       8          9     10      11        org.springframework.web.context.ContextLoaderListener12       13       14     15     16      contextConfigLocation17       18        classpath*:spring/applicationContext-*.xml19       20         21 22 

web.xml

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

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC1 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) FacesContext13                 .getCurrentInstance().getExternalContext().getContext();14         ApplicationContext appctx = WebApplicationContextUtils15                 .getRequiredWebApplicationContext(context);16 17         return appctx;18     }19 20     public static  T getBean(Class t) {21         return getApplicationContext().getBean(t);22     }23 }

ApplicationContextUtils

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

3. 使用properties文件注入

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC1 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      * 产品ID20      */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     @Override48     public String toString() {49         return productId + "/" + productNo + "/" + productName;50     }51 52 }

ProductEntity

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC1 <?xml  version="1.0" encoding="UTF-8"?> 2  6  7      9         10             11                 classpath:properties/*.properties12             13         14     15 16     17         18         19         20         26     27 

spring配置文件

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC1 product.id=32 product.no=n953 product.name=phone

product.properties

该文件被spring自动加载后 , 就可以用里面定义的属性值 , 为Bean做setter属性注入 , 即配置文件中的

4.验证注入是否成功

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC1 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 = ApplicationContextUtils14                 .getBean(ProductEntity.class);15 16         return product.toString();17     }18 19 }

HomeController

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC1 html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  2   6  7 

 8     jsf-web 9  10  11     

12         #{Home.sayHello()}13         14     

15  16 

index.xhtml

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值