spring注解@Value取不到值

spring注解@Value取不到值

@value取值,1用bean的方式配置

<bean id="propertyConfigurer"

  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="order" value="1" />

扫描二维码关注公众号,回复: 1086386 查看本文章

<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>

<property name="ignoreResourceNotFound" value="true"/>

<property name="ignoreUnresolvablePlaceholders" value="true"/>

<property name="locations">

<list>

<value>classpath:system.properties</value>

<value>classpath:redis.properties</value>

<value>classpath:dubbo.properties</value>

<value>classpath:cas.properties</value>

</list>

</property>

</bean>

2,在spring容器中管理的bean,如果在controller层需要在mvc的配置文件中在配置一次

在一个项目中发现一个情况,在Controller中取不到这个值,直接输出了${name}字符串,并没有解析出值,而在service中却能取到.有点奇怪啊,

明显在Controller中貌似并没有引入properties文件中的变量,而被当做普通的字符串处理了..突然想到这个项目有2个配置文件,

1个在WEB-INF下的springmvc-servlet.xml,1个在classpath下的applicationContext.xml,其中applicationContext.xml中定义有placeholder.

可以看到分为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配置文件时候,他们之间是不能互相访问的.

参考:

https://blog.csdn.net/jml1437710575/article/details/52020936

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2419529