我在Jar文件中使用Spring从属性文件中获取属性。当我尝试使用RAD(eclipse)时,我正在输出。但是当我在服务器中部署我的jar文件时,我不断收到此错误。我是否需要包含任何其他XML文件?
当我获得应用程序上下文时会发生错误。
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
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:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://xml.westfieldgrp.com/public/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/beans http://xml.westfieldgrp.com/public/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://xml.westfieldgrp.com/public/schema/jee/spring-jee-3.0.xsd" >
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/config/devint/nimo.properties"/>
</bean>
<bean id="nimoConfigurationBean" scope="singleton"
class="com.westfieldgrp.filenet.env.NimoConfigurationBean">
<property name="serviceUser" value="${env.user}" />
<property name="servicePass" value="${env.pass}" />
</bean>
</beans>
使用Java调用:
public class AddEnvProperty {
public String envType(String propertyValue) {
String returnValue = "";
AddEnvProperty envProps = new AddEnvProperty();
NimoConfigurationBean nimoConfigurationBean = envProps.getConfig();
PluginLogger logger = new PluginLogger(new ResponceFilterPlugin());
logger.logDebug(this, "envType", "Getting Property Value" + propertyValue);
try {
if (propertyValue == "USER") {
returnValue = nimoConfigurationBean.getServiceUser();
} else if (propertyValue == "PASS") {
returnValue = nimoConfigurationBean.getServicePass();
}
} catch (NullPointerException ex) {
logger.logError(this, "envType", "NullPointerException:", ex);
}catch (Exception ex) {
logger.logError(this, "envType", "NullPointerException:", ex);
}
return returnValue;
}
private NimoConfigurationBean getConfig() {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
NimoConfigurationBean obj = (NimoConfigurationBean) context.getBean("nimoConfigurationBean");
return obj;
}
}
Getter,NimoConfigurationBean.java中的setter方法
答案 0 :(得分:1)
检查ur pom文件中是否存在该依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>x.x.x.RELEASE</version>
</dependency>
(x.x.x:待修改)
如果你不使用maven:
检查jar“spring-context-x.x.x.RELEASE.jar
”到您的构建路径