properties文件路径没有错,tomcat启动正常,可是就是显示wingPath为null,这是为什么??第一次发帖,各路高手过来看下~
data.properties:
mallPath = store/mall.json
wingPath = wing/wing.json
applicationContext.xml:
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
<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
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, 本人个人理解是这样