开发者

@Autowired and PropertyPlaceholder

开发者 https://www.devze.com 2022-12-16 17:50 出处:网络
Is it possible to add a property from PropertyPl开发者_运维百科aceholder to a bean via @Autowired? I can\'t inject it in the xml-context-config because the beans are loaded this way:

Is it possible to add a property from PropertyPl开发者_运维百科aceholder to a bean via @Autowired? I can't inject it in the xml-context-config because the beans are loaded this way:

<context:component-scan base-package="..."/>


In spring 3.0 (I think from Milestone 3) you can use @Value("${foo.bar}") to access properties from PropertyPlaceholder.


A spring 2.5 approach:

@Component
public class Foo {
    @Autowired 
    @Qualifier("myFlag")
    private Boolean flag;
    /* ... */
}

and the context

<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>

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号