目前我了解的共有三种方式来引入property文件,必须要引入文件以后,才能使用${}的方式来获取其中的值
方式一:
在xml配置文件中加入
<bean id="pro" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:user.property</value>
</list>
</property>
</bean>
多个资源的时候,在list标签下面继续增加<value>即可
方式二:
在xml配置文件中加入
<context:property-placeholder location="classpath:user.property,classpath:user2.property"/>,多个资源可以用逗号隔开写在一起
方式三:
在配置类上进行添加注解
@PropertySource("user.property")
或者
@PropertySources({@PropertySource("user.property"),@PropertySource("user1.property")})
然后结合@value取值即可
如下
//@PropertySource("user.property")
@PropertySources({@PropertySource("user.property"),@PropertySource("user1.property")})
@Configuration
public class PropertiesCOnfig {
@Bean
public TestConfig getTestConfig(@Value("${user.name}") String name,@Value("${user.age}") String age) {
TestConfig t=new TestConfig();
t.setName(name);
t.setAge(age);
return t;
}
}