Spring通过注解@Value获取properties配置

来源:互联网 发布:js的单选按钮的事件 编辑:程序博客网 时间:2024/06/05 03:57

在applicationContext.xml中配置properties文件,那么在service层使用@Value注解即可访问到,但在Controller层使用@Value注解却不能访问到。若要在Controller层也使用@Value访问properties配置的话,需要在xxx-servlet.xml中也定义properties配置文件。


roperties文件在spring配置文件xml中定义如下:

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="locations">            <list>                <value>classpath:resource/*.properties</value>            </list>        </property></bean>


或者

<context:property-placeholder location="classpath:resource/*.properties"/>


db.properties文件:

dburl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8  dbusername=root  dbpassword=123456  

使用@Value访问:

@Value("${dburl}")  private String dburl;  



阅读全文
2 0