Code Bye

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;
	}
解决方案

5

IndexInitAction这个类没有声明为spring的组件,spring 启动的时候去讲全部配置的bean 实例化放在容器里,就像你的data.properties,所以IndexInitAction这个类拿不到容器里的bean

5

spring 的jar版本不对,你换成3.0以上的。

7

引用:
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类路径" />

10

引用:
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;
    }

7

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

6

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

是web的上下文,但是本人理解并没有加入DispatcherServlet, 本人个人理解是这样


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring利用注解@Value获取properties属性为null, 求指导~