Spring @Value 无法获取到properties文件中的值
2017-05-11 00:00
281 查看
摘要: 解析spring 中 @Value 获取值为null的错误
dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件
org.springframework.web.servlet.DispatcherServlet
这里最终是使用WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 创建spring容器,代码在FrameworkServlet中
applicationContext.xml 是另外一个,也需要注入属性文件
org.springframework.web.context.ContextLoaderListener
3.我们在的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml(spring.xml)中,这样在 Controller中是取不到的,必须在dispatcher-servlet.xml 中把获取属性文件再定义一下
spring-mvc.xml
spring-redis.xml
spring-mybatis.xml
因为系统启动时 spring-xml.xml 和 spring.xml,spring.xml-reids,spring-batis.xml 不是统一个容器,所以导致Controller无法通过@Value获取到(spring.xml,spring.xml-reids,spring-batis.xml)中关联的properties值。
在非Controller层无法通过@Value获取值
变量不能使用 static 修改spring mvc Controller中使用@Value无法获取属性值
在使用spring mvc时,实际上是两个spring容器:
dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件
org.springframework.web.servlet.DispatcherServlet
这里最终是使用WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 创建spring容器,代码在FrameworkServlet中
applicationContext.xml 是另外一个,也需要注入属性文件
org.springframework.web.context.ContextLoaderListener
3.我们在的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml(spring.xml)中,这样在 Controller中是取不到的,必须在dispatcher-servlet.xml 中把获取属性文件再定义一下
spring xml
spring.xmlspring-mvc.xml
spring-redis.xml
spring-mybatis.xml
因为系统启动时 spring-xml.xml 和 spring.xml,spring.xml-reids,spring-batis.xml 不是统一个容器,所以导致Controller无法通过@Value获取到(spring.xml,spring.xml-reids,spring-batis.xml)中关联的properties值。
相关文章推荐
- Spring之@Value 注解获取properties值(Spring 3的@value简化配置文件的读取)
- spring @Value 无法取值 多个properties文件
- spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别
- 关于spring使用@value注解获取properties文件属性问题
- Spring EmbeddedValueResolverAware 接口获取properties文件属性
- Spring+Mybatis多数据源配置(三)——Spring如何获取Properties文件的信息
- spring配置文件获取其他properties文件中的值及使用
- spring 之 PropertiesLoaderUtils 获取文件属性用法
- 关于tomcat下spring无法加载依赖jar中properties文件的原因分析
- SpringBoot获取yml和properties配置文件的内容
- Spring如何获取Properties文件的信息
- spring property value 引用properties文件和直接设置的区别
- Java中如何获取spring中配置的properties属性文件内容
- Spring获取properties中同一个key对应的多条value的方法
- spring无法读取properties文件数据
- Spring获取properties文件中的属性
- Spring3.x 获取properties资源文件的值
- Spring3.x 获取properties资源文件的值
- Java,spring程序中实时获取.properties属性key的value
- Java获取.properties配置文件某一项value根据key值