spring利用注解@Value获取properties属性为null, 求解~

手写白 2014-11-19 07:24:56

properties文件路径没有错,tomcat启动正常,可是就是显示wingPath为null,这是为什么??第一次发帖,各路大神过来看下~

data.properties:
mallPath = store/mall.json
wingPath = wing/wing.json

applicationContext.xml:

			
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- ====================== 将多个配置文件读取到容器中,交给Spring管理 ===================== -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/classes/data.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"></property>
</bean>


java:

import org.springframework.beans.factory.annotation.Value;
public class IndexInitAction {

@Value("#{configProperties['wingPath']}")
private String wingPath; // 这里就是得不到值,!

public String getWingPath() {
return wingPath;
}
public void setWingPath(String wingPath) {
this.wingPath = wingPath;
}
...全文
97351 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
2000风雨兼程 2018-09-26
  • 打赏
  • 举报
回复
@Value("${spring.profiles.active}")这种使用方式是依赖当前bean依赖注入才会有对应属性值注入进去的
rubber_ball 2017-07-03
  • 打赏
  • 举报
回复
在使用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 在我们的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml中,这样在 Controller中是取不到的,必须在dispatcher-servlet.xml 中把获取属性文件再定义一下
飞天破晓 2017-06-26
  • 打赏
  • 举报
回复
我也遇到过这个问题,后来发现是我的spring和springmvc的配置都没错,错的是service的包居然前缀有controller,于是springmvc扫了之后,值背覆盖了,最后在service中获取不到值,冒汗。我就在想,就算是父子容器,service包中的也springmvc应该可以获取得到啊,哈哈,还好找出来了
cyc123007512 2017-03-13
  • 打赏
  • 举报
回复
我也遇到这个问题,当时脑抽了写实现类的时候写了 TT t=new TT();换成@Autowired就可以了
一枚假程序猿 2017-01-03
  • 打赏
  • 举报
回复
@Value("${spring.profiles.active}") 不是 @Value("#{spring.profiles.active}")
Hrz-Alsace 2016-12-22
  • 打赏
  • 举报
回复
@PropertySource("classpath:application.properties")
linkw_92 2016-10-18
  • 打赏
  • 举报
回复
引用 32 楼 qq_34009965 的回复:
我也遇到了这个问题,后来解决了,原因是如果有注入bean的那个类,在被其他类作为对象引用的话(被调用)。这个被调用的类也必须选择注解的方式,注入到调用他的那个类中,不能用 new出来做对象,new出来的对象再注入其他bean就会 发生获取不到的现象。所以要被调用的javabean,都需要@service,交给Spring去管理才可以,这样他就默认注入了。
太好了,你说的正是我遇到的问题,已成功解决,谢谢。
sdylag 2016-04-25
  • 打赏
  • 举报
回复
晕,纠结了一天,终于在上面所有的方法中成功了。。。。 我是参考 34# 哥们,给出的链接 ,将: <bean id="configProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreResourceNotFound" value="true"/> <property name="locations"> <list> <value>classpath*:/system.properties</value> </list> </property> </bean> ---------------------------------------------- 请注意: 1 、 <value>classpath*:/system.properties</value> 这里的 classpath*: 后面带一个 / 2、 context:component-scan 扫描多个包 <context:component-scan base-package="com.aaa.service,com.aaa.util"/> 3、我用的就是在 util 包 工具类中使用 @Component 4、通过 @Value 注解获取配置文件中的值 @Value ("${pay.callbackUrl}")
layxcn2 2016-04-11
  • 打赏
  • 举报
回复
两种方法Xml配置 <util:properties location="classpath:db.properties"/> 或者生成一个静态Bean PropertySourcesPlaceholderConfigurer 实例 @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); }
wqiankunc 2016-03-12
  • 打赏
  • 举报
回复
我遇到过,我当时的情况是controller 全部交给springMVC 去扫描的,spring 配置文件里把注解扫描时没有扫描controller,所有取不到值得
千寻的等待 2016-03-05
  • 打赏
  • 举报
回复 2
我也遇到了这个问题,后来解决了,原因是如果有注入bean的那个类,在被其他类作为对象引用的话(被调用)。这个被调用的类也必须选择注解的方式,注入到调用他的那个类中,不能用 new出来做对象,new出来的对象再注入其他bean就会 发生获取不到的现象。所以要被调用的javabean,都需要@service,交给Spring去管理才可以,这样他就默认注入了。
千寻的等待 2016-03-04
  • 打赏
  • 举报
回复
引用 30 楼 baidu_19760977 的回复:
我的情况和楼主相似,但是我在controller中是成功获取到了值的,后来因为需要写一个工具类做编码操作,需要在这个工具类中用到@Value,但是却获取不到了 工具类 在配置文件中也使用了component-scan扫描了 <context:component-scan base-package="com.xwtech.mpsrm.server.core.controller,com.xwtech.mpsrm.util" /> 求解
我遇到了你一样的问题,请问楼上解决了吗?
magic_nian 2016-02-23
  • 打赏
  • 举报
回复
我的情况和楼主相似,但是我在controller中是成功获取到了值的,后来因为需要写一个工具类做编码操作,需要在这个工具类中用到@Value,但是却获取不到了
工具类


在配置文件中也使用了component-scan扫描了
<context:component-scan base-package="com.xwtech.mpsrm.server.core.controller,com.xwtech.mpsrm.util" />

求解
qq_25012195 2016-01-26
  • 打赏
  • 举报
回复
引用 23 楼 tianfeng198810qq 的回复:
您好,我今天也遇到这样的问题,通过查找资料,我这样解决了,可能你也是这样的问题。 在使用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 在我们的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml中,这样在 Controller中是取不到的,必须在dispatcher-servlet.xml 中把获取属性文件再定义一下 原文链接地址:http://sunjun041640.blog.163.com/blog/static/256268322014127113844746/
他和你不一样,看配置楼主连对象都还没注入呢
qq_25012195 2016-01-26
  • 打赏
  • 举报
回复
19楼开始终于答对了 @Component 并且 context:component-scan
树先生0_0 2016-01-15
  • 打赏
  • 举报
回复
你这样试试------------->>> import org.springframework.beans.factory.annotation.Value; public class IndexInitAction { @Value("${configProperties['wingPath']}") private String wingPath; // 这里就是得不到值,! public String getWingPath() { return wingPath; } public void setWingPath(String wingPath) { this.wingPath = wingPath; }
zhangchunru888 2015-12-22
  • 打赏
  • 举报
回复
请问楼主解决了么
Java__Coder 2015-12-15
  • 打赏
  • 举报
回复
楼主问题解决没有。。。
麥田稻草 2015-10-26
  • 打赏
  • 举报
回复
在xml中添加spring对注解的自动扫描配置:<context:component-scan base-package="com.**." />,没有这个spring不认识@Value
加载更多回复(20)

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧