spring 配置扫描怎么扫描class和配置文件

2666人阅读
Spring(36)
第一种情况是在测试类中读取配置文件中的值,实现如下:
1、在spring-mybatis配置文件中引入配置文件,代码如下:
&span style=&font-family:Comic Sans MS;&&
&!-- 引入配置文件 --&
&bean id=&prpertyConfigurer&
class=&org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&&
&property name=&locations&&
&value&classpath:jdbc.properties&/value&
&/property&
&/bean&&/span&
2、为了让controller读取到配置文件,仍需要在spring.xml文件中引入配置文件
&span style=&font-family:Comic Sans MS;&&&context:property-placeholder location=&classpath:jdbc.properties& /&&/span&
3、在测试类中写成如下形式:
&span style=&font-family:Comic Sans MS;&&@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={&classpath:spring-mybatis.xml&,&classpath:spring.xml&})
public class TestMyBatis{
//这是对应配置文件中的变量,给变量注入值
@Value(&${picUrl}&)
private String picU
。。。。。。&/span&
这样便自动将配置文件中picUrl变量的值注入给了picUrl,可以测试打印输出其值,发现就是配置文件中的值。
第二种情况是在Controller中读取配置文件中的值
之前我写了两个配置文件,想同时加载,但是发现一直出现各种问题。原来在spring.xml文件中写两个这样的:
&span style=&font-family:Comic Sans MS;&&
&context:property-placeholder location=&classpath:jdbc.properties& /&
&context:property-placeholder location=&classpath:picPath.properties&/&&/span&这样是行不通的。
后来我将这两个配置文件的内容合并,保存在了jdbc.properties中,便可以在Controller利用@Value对属性值进行注入了。
看看我的Controller类的写法:
&span style=&font-family:Comic Sans MS;&&@Controller
@Configuration
@ImportResource(&classpath:spring.xml&)
@RequestMapping(&/photo&)
public class FileUploadController {
@RequestMapping(value=&/tofile&)
public String toFileUpLoad(HttpServletRequest request,Model model){
return &fileUpLoad&;
//@Value(&#{settings['picPath.picUrl']}&)
@Value(&${picUrl}&)
String picU
public void setPicUrl(String picUrl) {
this.picUrl = picU
注意一定要引入配置文件,在类名上加上,如下两个注解,不然给picUrl变量注入的值不是配置文件中的值,而是字符串“${picUrl}”
&span style=&font-family:Comic Sans MS;&&&strong&@Configuration
@ImportResource(&classpath:spring.xml&)&/strong&&/span&
下面来分析下之前分散到两个配置文件中加载不成功的原因:
比如有A和B两个模块
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件
A模块的spring配置文件如下:
&span style=&font-family:Comic Sans MS;&&&?xml version=&1.0& encoding=&UTF-8& ?&
&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xmlns:context=&http://www.springframework.org/schema/context&
xmlns:p=&http://www.springframework.org/schema/p&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd&&
&context:property-placeholder location=&classpath*:conf/conf_a.properties&/&
&bean class=&com.xxx.&span style=&color:#cc0000;&&aaa.Bean1&/span&&
p:driverClassName=&${modulea.jdbc.driverClassName}&
p:url=&${modulea.jdbc.url}&
p:username=&${modulea.jdbc.username}&
p:password=&${modulea.jdbc.password}&/&
&/beans& &/span&
其配置文件位于类路径conf/conf_a.properties中:
&span style=&font-family:Comic Sans MS;&&modulea.jdbc.driverClassName=com.mysql.jdbc.Driver
modulea.jdbc.username=cartan
modulea.jdbc.password=superman
modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8&/span&
B模块的Spring配置文件如下:
&span style=&font-family:Comic Sans MS;&&&?xml version=&1.0& encoding=&UTF-8& ?&
&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xmlns:context=&http://www.springframework.org/schema/context&
xmlns:p=&http://www.springframework.org/schema/p&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd&&
&context:property-placeholder location=&classpath*:conf/conf_b.properties&/&
&bean class=&com.xxx.&span style=&color:#cc0000;&&bbb.Bean1&/span&&
p:driverClassName=&${moduleb.jdbc.driverClassName}&
p:url=&${moduleb.jdbc.url}&
p:username=&${moduleb.jdbc.username}&
p:password=&${moduleb.jdbc.password}&/&
其配置文件位于类路径conf/conf_b.properties中:
&span style=&font-family:Comic Sans MS;&&moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver
moduleb.jdbc.username=cartan
moduleb.jdbc.password=superman
moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8&/span&问题出现了,单独运行A模块或B模块都是正常,但是将A和B两个模块集成后运行,Spring容器就启动不了:
出现的问题:Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value &${moduleb.jdbc.driverClassName}&
问题的原因是:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean
就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring3.1已经使用PropertySourcesPlaceholderConfigurer替代
PropertyPlaceholderConfigurer了)。
而&context:property-placeholder/&这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,
即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或&context:property-placeholder),其余的会被Spring忽略掉。
拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer,因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了,&
先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。
解决办法:
属性文件加载在统一的地方做,不需要分模块加载
A模块a.xml:
&span style=&font-family:Comic Sans MS;&&&?xml version=&1.0& encoding=&UTF-8& ?&
&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xmlns:context=&http://www.springframework.org/schema/context&
xmlns:p=&http://www.springframework.org/schema/p&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd&&
&!--&context:property-placeholder location=&classpath*:conf/conf_a.properties&/&--&
&bean class=&com.xxx.&strong&&span style=&color:#cc0000;&&aaa.Bean1&/span&&/strong&&
p:driverClassName=&${modulea.jdbc.driverClassName}&
p:url=&${modulea.jdbc.url}&
p:username=&${modulea.jdbc.username}&
p:password=&${modulea.jdbc.password}&/&
B模块b.xml:
&span style=&font-family:Comic Sans MS;&&&?xml version=&1.0& encoding=&UTF-8& ?&
&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xmlns:context=&http://www.springframework.org/schema/context&
xmlns:p=&http://www.springframework.org/schema/p&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd&&
&!--&context:property-placeholder location=&classpath*:conf/conf_b.properties&/&--&
&bean class=&com.xxx.&strong&&span style=&color:#ff0000;&&bbb.Bean1&/span&&/strong&&
p:driverClassName=&${moduleb.jdbc.driverClassName}&
p:url=&${moduleb.jdbc.url}&
p:username=&${moduleb.jdbc.username}&
p:password=&${moduleb.jdbc.password}&/&
&/beans&&/span&
&span style=&font-family:Comic Sans MS;&&&?xml version=&1.0& encoding=&UTF-8& ?&
&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xmlns:context=&http://www.springframework.org/schema/context&
xmlns:p=&http://www.springframework.org/schema/p&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd&&
&context:property-placeholder location=&classpath*:conf/conf*.properties&/&
&import resource=&a.xml&/&
&import resource=&b.xml&/&
&/beans& &/span&
Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。&
这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,
而外面又更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用&context:property-placeholder/&引用到JAR中的属性文件,只要Spring配置文件中显示提供一个&context:property-placeholder/&指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:100763次
积分:2190
积分:2190
排名:第14680名
原创:117篇
转载:45篇
(3)(4)(3)(1)(3)(11)(10)(4)(5)(3)(4)(3)(5)(6)(3)(4)(5)(15)(8)(9)(15)(28)(10)}

我要回帖

更多关于 spring扫描配置文件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信