SpringMVC中有两个xml配置文件:
注:${user.dir}为tomcat下的bin目录,不用定义可以直接使用
使用@Value访问:
1、applicationContext.xml,这个是Spring的主配置文件,包括dao层service层的bean定义或扫描、数据源、事务等的配置信息。
2、xxx-servlet.xml,这个是mvc的配置文件,包括controller层的bean定义或扫描、静态资源访问以及view配置
properties配置文件中的信息定义在applicationContext.xml中,那么在service层使用@Value注解即可访问到,但在Controller层使用@Value注解却不能访问到。
若要在Controller层也使用@Value访问properties配置的话,需要在xxx-servlet.xml中也定义properties配置文件。
properties文件在spring配置文件xml中定义如下:
- <context:property-placeholder
- location="file:${user.dir}/persona/conf/datasource.properties,file:${user.dir}/persona/conf/system.properties"
- ignore-unresolvable="true" />
注:${user.dir}为tomcat下的bin目录,不用定义可以直接使用
扫描二维码关注公众号,回复:
1825877 查看本文章
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;