spring加载配置文件

在项目中有些参数经常需要修改,或者后期可能会有改动时,那我们最好把这些参数放到properties文件中,在源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源码。下面讨论spring两种加载方式,基于xml和基于注解的加载方式。

1. 通过xml方式加载properties文件

以Spring实例化dataSource为例,先在工程目录的src下新建一个conn.properties文件,里面写上上面dataSource的配置:

dataSource=com.mchange.v2.c3p0.ComboPooledDataSource  
driverClass=com.mysql.jdbc.Driver  
jdbcUrl=jdbc:mysql://localhost:3306/shop 
username=root  
password=root  

然后在只需要在beans.xml中做如下修改即可:

 <context:property-placeholder location="classpath:conn.properties"/><!-- 加载配置文件 -->  
  
 <!-- com.mchange.v2.c3p0.ComboPooledDataSource类在c3p0-0.9.5.1.jar包的com.mchange.v2.c3p0包中 -->  
 <bean id="dataSource" class="${dataSource}"> <!-- 这些配置Spring在启动时会去conn.properties中找 -->  
    <property name="driverClass" value="${driverClass}" />  
    <property name="jdbcUrl" value="${jdbcUrl}" />  
    <property name="user" value="${user}" />  
    <property name="password" value="${password}" />  
 </bean>  

<context:property-placeholder location=""/>标签也可以用下面的标签来代替,可读性更强:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations"> <!-- PropertyPlaceholderConfigurer类中有个locations属性,接收的是一个数组,即我们可以在下面配好多个properties文件 -->  
        <array>  
            <value>classpath:conf.properties</value>  
        </array>  
    </property>  
</bean>

2. 通过注解方式加载properties文件

首先新建一个资源文件:public.properties

shop.url=http://magic/shop

第一种配置方式:

	<!-- 确保可在@Value中, 使用SeEL表达式获取资源属性 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="properties" ref="configProperties" />
	</bean>

	<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>classpath:config/*.properties</value>
			</list>
		</property>
	</bean>

在java代码中用@Value获取配置属性值

	@Value("${shop.url}")
    private String url;

还有一种方式更简洁:

	<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
		<property name="fileEncoding" value="UTF-8"/> 
	    <property name="locations"><!-- 这里是PropertiesFactoryBean类,它也有个locations属性,也是接收一个数组,跟上面一样  
	        <array>  
	            <value>classpath:public.properties</value>  
	        </array>  
	    </property>  
	</bean> 
	<!--或者-->
	<context:property-placeholder location="classpath:public.properties" />  
	
	//注意,这种表达式要有set方法才能被注入进来,注解写在set方法上即可
	private String url;  
    @Value("#{prop.shop.url}")   
    //@Value表示去beans.xml文件中找id="prop"的bean,它是通过注解的方式读取properties配置文件的,然后去相应的配置文件中读取key=shop.url的对应的value值  
    public void setUrl(String url) {  
        this.token= url;  
    }

3.通过 @PropertySource和@Value 来读取配置文件

举个栗子:

@Component
@PropertySource(value = {"classpath:common.properties", "classpath:abc.properties"})
public class Configs {

    @Value("${connect.api.apiKeyId}")
    public String apiKeyId;

    @Value("${connect.api.secretApiKey}")
    public String secretApiKey;

    public String getApiKeyId() {
        return apiKeyId;
    }

    public String getSecretApiKey() {
        return secretApiKey;
    }
}

我们来具体分析下:

1、@Component注解说明这是一个普通的bean,在Component Scanning时会被扫描到并被注入到Bean容器中;我们可以在其它引用此类的地方进行自动装配。@Autowired这个注解表示对这个bean进行自动装配。 比如:

@Controller
public class HomeController {

    @Autowired
    private Configs configs;
}

2、@PropertySource注解用来指定要读取的配置文件的路径从而读取这些配置文件,可以同时指定多个配置文件;

3、@Value("${connect.api.apiKeyId}")用来读取属性key=connect.api.apiKeyId所对应的值并把值赋值给属性apiKeyId;


后续会补充static属性的注入方式,期待吧!!!

附:关于配置文件加载问题

posted @ 2018-02-02 19:28  总被人想的静静  阅读(12729)  评论(0编辑  收藏  举报