当前位置: 代码迷 >> J2EE >> spring利用引语@Value获取properties属性为null, 求解
  详细解决方案

spring利用引语@Value获取properties属性为null, 求解

热度:585   发布时间:2016-04-17 23:19:52.0
spring利用注解@Value获取properties属性为null, 求解~

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;
}

------解决思路----------------------
IndexInitAction这个类没有声明为spring的组件,spring 启动的时候去讲所有配置的bean 实例化放在容器里,就像你的data.properties,所以IndexInitAction这个类拿不到容器里的bean
------解决思路----------------------
spring 的jar版本不对,你换成3.0以上的。
------解决思路----------------------
引用:
public static String getDirPath() {
Resource resource = null;
Properties props = null;
String driverClass = null;
try {
resource = new ClassPathResource("/data.properties");
props = PropertiesLoaderUtils.loadProperties(resource);
driverClass= (String) props.get("wingPath");
} catch (IOException e) {
e.printStackTrace();
}
return driverClass;

}


我已经改成这种方式读取了. 可是不甘心啊~



<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="IndexInitAction类路径" />



------解决思路----------------------
引用:
Quote: 引用:

public static String getDirPath() {
Resource resource = null;
Properties props = null;
String driverClass = null;
try {
resource = new ClassPathResource("/data.properties");
props = PropertiesLoaderUtils.loadProperties(resource);
driverClass= (String) props.get("wingPath");
} catch (IOException e) {
e.printStackTrace();
}
return driverClass;

}


我已经改成这种方式读取了. 可是不甘心啊~



<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="IndexInitAction类路径" />







public class IndexInitAction {
     
    
    public String wingPath; // 这里就是得不到值,!
     
    public String getWingPath() {
        return wingPath;
    }

    @Value("#{configProperties['wingPath']}")
    public void setWingPath(String wingPath) {
        this.wingPath = wingPath;
    }


------解决思路----------------------
楼上说得对,你需要在DispatcherServlet 所在配置,写入
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/plugConfig.properties" />

------解决思路----------------------

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
     classpath*:/applicationContext.xml
    </param-value>
  </context-param>

是web的上下文,但是我理解并没有加入DispatcherServlet, 我个人理解是这样
  相关解决方案