-
Notifications
You must be signed in to change notification settings - Fork 7
Spring @Value用法
Landy.Liu edited this page May 26, 2019
·
1 revision
- @Value("#{configProperties['key']}")
- @Value("${key}")
配置方法1.
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:value.properties</value>
</list>
</property>
</bean>
配置方法2
<util:properties id="configProperties" location="classpath:value.properties"></util:properties>
注:配置1和配置2等价,这种方法需要util标签,要引入util的xsd:
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
value.properties
key=1
@Component
public class ValueDemo {
@Value("#{configProperties['key']}")
private String value;
public String getValue() {
return value;
}
}
方法1:在1.2.1.1的配置文件基础上增加:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"/>
</bean>
方法2:直接指定配置文件,完整的配置:
<bean id="appProperty"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:value.properties</value>
</array>
</property>
</bean>
@Component
public class ValueDemo {
@Value("${key}")
private String value;
public String getValue() {
return value;
}
}
SegmentFault: https://segmentfault.com/u/landy8530
简书:https://www.jianshu.com/u/36a7d3a994ac
CSDN:https://blog.csdn.net/landy8530
开源中国:https://my.oschina.net/landy8530
微信公众号:蚂蚁与咖啡的故事
Core组件
DataCache组件
Nosql组件
Export组件
文件服务组件
操作指引