使用spring mvc Controller中使用@Value无法获取属性值
来源:互联网 发布:短信猫 java 编辑:程序博客网 时间:2024/06/05 04:35
在spring.xml配置了<context:property-placeholder />,但是Controller中使用@Value拿不到属性值
原因为在使用spring mvc时,实际上是两个spring容器:
1,spring-mvc.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件org.springframework.web.servlet.DispatcherServlet这里最终是使用WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 创建spring容器,代码在FrameworkServlet中
2,applicationContext.xml 是另外一个,也需要注入属性文件org.springframework.web.context.ContextLoaderListener
在我们的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml中,这样在 Controller中是取不到的,必须在spring-mvc.xml中把获取属性文件再定义一下
<context:property-placeholder/>
和<util:properties/>
的区别。
<context:property-placeholder/>
是将properties加载到spring上下文中,接下来在定义bean的时候就能用${xx.xx}
来访问了。<util:properties/>
是org.springframework.beans.factory.config.PropertiesFactoryBean
的简化,声明了一个bean,可以用于注入。
阅读全文
0 0
- spring mvc Controller中使用@Value无法获取属性值
- spring mvc Controller中使用@Value无法获取属性值
- 使用spring mvc Controller中使用@Value无法获取属性值
- spring mvc,在controller中获取属性文件的值
- spring mvc controller中获取属性文件(properties)的值
- spring mvc controller中获取属性文件(properties)的值
- spring mvc中controller使用介绍
- spring mvc中mvc:view-controller的使用
- spring mvc controller 获取不到@Value处理方法
- Controller 使用RestTemplate进行单元测试 spring mvc
- 使用MockMvc测试Spring mvc Controller
- 使用MockMvc测试Spring mvc Controller
- spring mvc 使用junit 测试 controller
- 使用MockMvc测试Spring mvc Controller
- springmvc 属性放数据库中解决方法 以及 @Controller 中 使用@vlaue无法注入属性值问题详解
- Spring MVC 使用ajax时,控制器无法获取客户端参数
- spring mvc在非Controller类无法使用Service bean解决方案
- Spring mvc jsp/html等静态资源/Controller无法使用总结
- Liunx — 进程等待 (wait和waitpid函数)
- Eclipse创建Maven Web项目
- Java并发编程:volatile关键字解析
- JAVA垃圾回收详解(算法篇)
- Java开发中的23种设计模式详解----组合模式(Composite)
- 使用spring mvc Controller中使用@Value无法获取属性值
- Linuxg++编译C++代码出错 -std=c++11 or -std=gnu++11
- 深入java(类)接口默认修饰符问题
- 值得经常逛的一些网站
- iOS 布局篇(一)-NSLayoutConstraint的使用和 VFL 语言介绍
- word search
- 股票市场
- rpm查询安装包命令集合
- scala学习笔记(二)