spring配置中调用properties文件

. system.properties


database.url=jdbc:mysql://localhost/smaple
database.driver=com.mysql.jdbc.Driver
database.user=root
database.password=root

2.applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>>classpath:system.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${database.url}</value>
</property>
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="username">
<value>${database.user}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>

</bean>
</beans>

当放入多个配置文件时
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>>classpath:system1.properties</value>
<value>>classpath:system2.properties</value>
</list>
</property>
</bean>

还可以这样
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:*.properties</value>
</list>
</property>
</bean>

 

 

在Spring中,使用PropertyPlaceholderConfigurer和PropertyOverrideConfigurer可以在XML配置文件中加入外部属性文件
但使用这种方式,有一些需要注意的地方

1.首先在主类中,需要使用ClassPathXmlApplicationContext来读取spring配置xml文件
如:

ApplicationContext context = new ClassPathXmlApplicationContext("example4/appcontext.xml");
HelloWorld hw = (HelloWorld)context.getBean("fileHelloWorld");
log.info(hw.getContent());

直接以beanFactory方式,是无法使用PropertyPlaceholderConfigurer或PropertyOverrideConfigurer的
如下方式不行:
Resource resource = new ClassPathResource("example4/appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloWorld hw = (HelloWorld) factory.getBean("fileHelloWorld");
log.info(hw.getContent());

2.PropertyOverrideConfigurer需要考虑bean的名称
如下是正确配置:
appcontext.xml:
<bean name="fileHelloWorld" class="example4.HelloWorld">
    <constructor-arg>
        <ref bean="fileHello"/>
    </constructor-arg>
    <property name="statusname">
        <value>${fileHelloWorld.statusname}</value>
    </property>
</bean>

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
    <property name="location" value="classpath:example4/helloworld.properties"/>
</bean>

helloworld.properties:
fileHelloWorld.statusname=this is status Name;

如果少了${fileHelloWorld.statusname}中少了Bean名称fileHelloWorld,会导致错误发生
这应该是种强制某个配置是属性某个Bean

3.PropertyPlaceholderConfigurer的配置不需要考虑Bean的名称,直接配置就可以了
配置方式和PropertyOverrideConfigurer类似
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值