博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring @Value 无法获取到properties文件中的值
阅读量:6951 次
发布时间:2019-06-27

本文共 764 字,大约阅读时间需要 2 分钟。

hot3.png

在非Controller层无法通过@Value获取值

  1. 变量不能使用 static 修改

spring mvc Controller中使用@Value无法获取属性值

在使用spring mvc时,实际上是两个spring容器:
  1. dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件 org.springframework.web.servlet.DispatcherServlet 这里最终是使用WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 创建spring容器,代码在FrameworkServlet中
  2. applicationContext.xml 是另外一个,也需要注入属性文件 org.springframework.web.context.ContextLoaderListener Controller中是取不到的,必须在dispatcher-servlet.xml 中把获取属性文件再定义一下

spring xml

  • spring.xml
  • spring-mvc.xml
  • spring-redis.xml
  • spring-mybatis.xml 因为系统启动时 spring-xml.xml 和 spring.xml,spring.xml-reids,spring-batis.xml ,spring.xml-reids,spring-batis.xml)中关联的properties值。

转载于:https://my.oschina.net/shichangcheng/blog/897374

你可能感兴趣的文章
PHP服务器负载判断
查看>>
爬取猎聘大数据岗位相关信息--Python
查看>>
HDU-3072-IntelligenceSystem(tarjan,贪心)
查看>>
upload控件
查看>>
【Foreign】Weed [线段树]
查看>>
js实现轮播图常规类(原生JS,没有任何框架)
查看>>
快速上手Git
查看>>
求符合给定条件的整数集(15)
查看>>
在字符串中查找指定字符(15)
查看>>
jdk及tomcat的安装
查看>>
hbase常识及habse适合什么场景
查看>>
JAVA 中一个非常轻量级只有 200k 左右的 RESTful 路由框架
查看>>
2018.8.5 复习笔记
查看>>
【转】 DOTA2中的伪随机及其lua实现
查看>>
A*算法、导航网格、路径点寻路对比(A-Star VS NavMesh VS WayPoint)
查看>>
sys
查看>>
webSQL 实现即时通讯
查看>>
Monkey学习笔记<三>:Monkey脚本编写
查看>>
tomcat监听activemq jms配置
查看>>
页面中引入js的几种方法
查看>>