spring注解@Value取不到值【转】
spring注解@Value取不到值
今天在一个项目中发现一个情况,在Service中取不到name值,直接输出了{name}字符串,找了好久,最后在一篇文章中找到解决方案.
解决这个问题的一篇文章(转)
@Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml
项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置:
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" />
这样在程序代码中直接用@Value(“${name}”)就能直接取到properties文件中定义的变量值.
但是在一个项目中发现一个情况,在Controller中取不到这个值,直接输出了${name}字符串,并没有解析出值,而在service中却能取到.有点奇怪啊,明显在Controller中貌似并没有引入properties文件中的变量,而被当做普通的字符串处理了..突然想到这个项目有2个配置文件,1个在WEB-INF下的springmvc-servlet.xml,1个在classpath下的applicationContext.xml,其中applicationContext.xml中定义有placeholder.
说实话之前并没有注意过这个配置文件的区别,一直以为只是放置的位置不一样而已,借助这次机会吧,查询到了一些资料.先看一则引入spring在web.xml中的配置:
<!-- springmvc配置开始 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- springmvc配置结束 --> <!-- Spring配置开始 -->
<listener>
<listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param> <!-- Spring配置结束 -->
可以看到分为spring配置和springmvc配置2种.其中spring配置以监听器的形式引入,不指定xml配置文件地址则默认查找WEB-INF下的applicationContext.xml文件.springmvc则以 servlet形式引入,当没有指定引入的xml配置文件地址时,则会自动引入WEB-INF下的[servlet-name]-servlet.xml文件,本例中为springmvc-servlet.xml.引入顺序为先引入spring配置,再引入servlet形式的springmvc配置.
值得注意的几点是:springmvc的配置文件中可以直接用id引入spring配置文件中定义的bean,但是反过来不可以.每一个springmvc的配置文件xxx-servlet.xml对应一个web.xml中的servlet定义.当存在多个springmvc配置文件时候,他们之间是不能互相访问的.
在百度中别人的帖子中看到一段应该是官方的原文解释,我摘抄过来并粗糙的直译一下:
Spring lets you define multiple contexts in a parent-child hierarchy.
spring允许你定义多个上下文在父子继承关系中
The applicationContext.xml defines the beans for the “root webapp context”, i.e. the context associated with the webapp.
applicationContext.xml文件是为了”根webapp应用上下文”定义bean, 也就是说它的上下文是和webapp想关联的
The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet’s app context. There can be many of these in a webapp,
spring-servlet.xml文件(或是其他的你习惯的称呼)是为了一个servlet应用上下文呢定义bean. 在一个webapp中可以有多个此配置文件,
one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).
每一个spring的servlelt(例如: 名为spring1的servlet拥有配置文件spring1-servlet.xml, 名为spring2的servlet拥有配置文件spring2-servlet.xml).
Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.
在spring-servlet.xml中定义的bean可以直接引用在applicationContext.xml中定义的bean, 但是反过来不可以.
All Spring MVC controllers must Go in the spring-servlet.xml context.
所有springmvc的Controller必须在spring-servlet.xml对应的上下文中运行.
In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets
在大多数简单的情况下, applicationContext.xml对应的上下文并不必须. 它通常用来包含那些bean用来在webapp中所有servlet之间共享.
in a webapp. If you only have one servlet, then there’s not really much point, unless you have a specific use for it.
如果你只有一个servlet, 那么实际没有什么必要定义applicationContext.xml, 除非你有特别应用.
那么回到最开始的@Value取不到值的问题,现在的可以清楚由于Controller是定义在springmvc的servlet配置文件中的,查找,故只需要将placeholder重新在springmvc的配置中配置一遍,Controller中的@Value注解便能取到值了.
转载:http://blog.csdn.net/jml1437710575/article/details/52020936
spring注解@Value取不到值【转】的更多相关文章
- Spring中@Value("${}"))取不到值的几种情况
https://blog.csdn.net/dh12313012/article/details/84661169 1. spring组件重写构造方法,在构造方法中引用@Value为null 由于sp ...
- @Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml
项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置: <context:property-placeholder ignore ...
- spring@value取不到值的几种情况
一,spring组件重写构造方法,在构造方法中引用@value为null 由于spring实例化顺序为先执行构造方法,再注入成员变量,所以序为先执行构造方法,再注入成员变量,所以ing实例化顺取值为n ...
- 控制层@Value注解取不到值
@Value("${enable-upload-image}") private String enable; 如上所示,同样的代码,写在在业务层,运行时能取到正确的值,但在控制层 ...
- spring注解总结
• @Controller 表示 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写,表示某类是一个控制器组件 • @Service 表示负责注册一个bea ...
- Spring注解 @Configuration
Spring注解 @Configuration 一.@Configuration的作用 二.@Configuration的Spring容器启动方式 三.不加@Configuration的@Bean的解 ...
- Struts2+Spring3+MyBatis3整合以及Spring注解开发
分类: Web(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在做一个SpringMVC+spring+MyBatis的项目,突然想起以前自己要搭建一个Struts2+Sprin ...
- Spring 获取propertise文件中的值
Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...
- Spring注解和标签的比较说明
待完善.... xml标签 注解 说明 xml的Spring约束头 @Configuration xml约束头表明这是用于spring的的配置文件 @Configuration注解表情这是用于Spri ...
随机推荐
- 【monkeyrunner】monkeyrunner 的主要API和实例
MonkeyRunner简介 monkeyrunner工具提供了编写控制Android设备或仿真器从Android的代码之外程序的API.随着monkeyrunner,您可以编写安装一个Android ...
- 常见的JS和CSS问题
事件冒泡 DOM的事件冒泡机制和WPF很相似,DOM事件机制包含冒泡和捕获两种,按照topmost element->innermost element方向传递事件被称为捕获方式,而从inner ...
- android studio 3.0.1使用笔记(一)20171231
首先安装JDK1.8.1并设置环境变量JAVA_HOME(C:/JAVA/JDK)及PATH(;%JAVA_HOME%\bin ) 然后安装AS 然后首次AS运行并退出,将GRADLE-4-1.ZIP ...
- Navicat 入门使用方法
Navicat 多重连接数据库的管理工具,支持连接到(MySQL.Oracle.PostgreSQL.SQLite .MariaDB )多类数据库,也支持多类数据库的管理和使用 1.Navicat 主 ...
- maven 编译解决jdk 版本问题
1.在父工程中pom 添加版本限制: <plugins> <plugin> <groupId>org.apache.maven.plugins</groupI ...
- leetcode225
public class MyStack { Queue<int> Q = new Queue<int>(); /** Initialize your data structu ...
- maven+testng+reportng的pom设置
在pom.xml 加入: <dependency> <groupId>org.testng</groupId> <artifactId>testng&l ...
- Linux 移除python Error: Trying to remove “yum”, which is protected
>yum intall python >yum -y remove python 出现Error: Trying to remove "yum", which is p ...
- 在MetaFile里放图片
procedure TForm1.Button1Click(Sender: TObject); var m : TmetaFile; mc : TmetaFileCanvas; b : tbitmap ...
- pg_hba.conf、pool_hba.conf 以及 pool_passwd 三者间的关系
pg_hba.conf.pool_hba.conf 以及 pool_passwd 三者间的关系: 1.pg_hba.conf.pool_hba.conf 以及 pool_passwd 三者关系 pg_ ...