Java @自动接线和PropertyPlaceholder
是否可以通过@Autowired将PropertyPlaceholder中的属性添加到bean中?我无法将其插入xml上下文配置中,因为bean的加载方式如下:Java @自动接线和PropertyPlaceholder,java,spring,Java,Spring,是否可以通过@Autowired将PropertyPlaceholder中的属性添加到bean中?我无法将其插入xml上下文配置中,因为bean的加载方式如下: <context:component-scan base-package="..."/> 在Spring3.0中(我认为是里程碑3),您可以使用@Value(${foo.bar})从PropertyPlaceholder访问属性。Spring2.5方法: @Component public class Foo {
<context:component-scan base-package="..."/>
在Spring3.0中(我认为是里程碑3),您可以使用@Value(${foo.bar})从PropertyPlaceholder访问属性。Spring2.5方法:
@Component
public class Foo {
@Autowired
@Qualifier("myFlag")
private Boolean flag;
/* ... */
}
以及背景
<context:component-scan base-package="..."/>
<context:property-placeholder location="classpath:app.properties"/>
<!-- the flag bean -->
<bean id="myFlag" class="java.lang.Boolean">
<constructor-arg value="${foo.bar}"/>
</bean>
干杯你能给我举个例子吗?我正在使用Spring3RC3,当我尝试这种方式时(例如@Value(${foo.bar})),我的属性就会得到值“foo.bar”…是的,假设您有如下配置的属性占位符:
您可以从app.properties
向您的bean注入带有keyfoo.bar
的属性值:类MyBean{@Value(${“foo.bar”}私有字符串值;}
+1或者,更详细一点,@Resource(name=“myFlag”)