Spring3中用注解直接注入properties中的值

来源:互联网 发布:雕刻大师软件 编辑:程序博客网 时间:2024/06/05 14:48
在bean(可是controller, service, dao等了)中,使用@Value注解:

@Service

public class TestService{

@Value("${sytem.username}")String loginUserName;

}

在spring中定义资源文件:
<context:property-placeholder />或者org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><array><value>classpath:config.properties</value></array></property></bean>
config.properties文件:

sytem.username=admin

这样config.properties中的sytem.username就注入到loginUserName属性中了
0 0
原创粉丝点击