Spring 通过注解注入Properties文件

来源:互联网 发布:2017年最新java面试题 编辑:程序博客网 时间:2024/05/16 09:00

原来一直使用spring xml来管理properties 的 placeholder, 这种方式造成无法使用Spring注解方式注入, 造成了xml的代码冗余, 现在Spring提供了一种更加优雅的方式来配合annotation的注入方式, 详见代码


原本是要这么写的 xml

<context:property-placeholder            location="            classpath:test.properties"/>    <bean id="test" class="test.FactoryBean">        <property name="servers" value="${session.memcached.servers}"/>    </bean>


现在只需要

@Componentpublic class FactoryBean{@Value("${"session.memcached.servers"}")    private String host;}





原创粉丝点击