在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"/>
dburl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
dbusername=root
dbpassword=123456
使用@Value访问:
@Value("${dburl}")
private String dburl;