properties文件路径没有错,tomcat启动正常,可是就是显示wingPath为null,这是为什么??第一次发帖,各路大神过来看下~ data.properties: 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; } |
|
<value>/WEB-INF/classes/data.properties</value>
改成: <value>classpath*:data.properties</value> |
|
不行呀, 试过了的. 路径是是没有问题的, 如果路径有问题, 启动tomcat的时候就会报错. |
|
5分 |
IndexInitAction这个类没有声明为spring的组件,spring 启动的时候去讲所有配置的bean 实例化放在容器里,就像你的data.properties,所以IndexInitAction这个类拿不到容器里的bean
|
那我要咋做?怎么声明为spring的组件? |
|
@Value(“#{data.wingPath}”)
|
|
也不行. 这到底是为啥 |
|
@Value(“${wingPath}”)
|
|
也取不到值, 到底是哪个环节出问题了呢 |
|
5分 |
spring 的jar版本不对,你换成3.0以上的。
|
我把spring-一系列的jar包都换成3.0的了, applicationContext.xml 的xsi配置也改成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; } 我已经改成这种方式读取了. 可是不甘心啊~ |
|
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, 我个人理解是这样 |