java - 属性文件中的值返回空

  显示原文与译文双语对照的内容

在外部。properties文件中设置了一个变量,但当我尝试调用值时,它仍然返回 null 。 我们还有一个正在导入外部。属性文件的项目,但即使使用这个模型,我也不知道为什么。 以下相关文件。

返回NULL的具体内容是 PropertiesUtil.getProperty("property.file.variable");

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">

 <display-name>PROJECT</display-name>

 <servlet>
 <servlet-name>Jersey REST Service</servlet-name>
 <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
 <init-param>
 <param-name>com.sun.jersey.config.property.packages</param-name>
 <param-value>com.bar.foo.rest</param-value>
 </init-param>
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>Jersey REST Service</servlet-name>
 <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>

 <listener>
 <listener-class>com.bar.foo.rest.ListenerClass</listener-class>
 </listener>

</web-app>

applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <tx:annotation-driven/>
 <context:annotation-config/>
 <mvc:annotation-driven/>

 <context:component-scan base-package="com.bar.foo;"/>

 <context:property-placeholder location="file:${externalConfiguration}"/>

 <bean id="propertyConfigurer" class="com.bar.foo.utils.PropertiesUtil">
 <property name="ignoreResourceNotFound" value="true"/>
 <property name="ignoreUnresolvablePlaceholders" value="true"/>
 <property name="locations">
 <list>
 <value>classpath:PROJECT.properties</value>
 <value>file:${externalConfiguration}</value>
 </list>
 </property>
 </bean>

 <bean id="configProperties"
 class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 <property name="ignoreResourceNotFound" value="true"/>
 <property name="locations">
 <list>
 <value>classpath:PROJECT.properties</value>
 <value>file:${externalConfiguration}</value>
 </list>
 </property>
 </bean>
</beans>

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
 <groupId>com.bar</groupId>
 <artifactId>PROJECT</artifactId>
 <version>1.30.1-SNAPSHOT</version>
 </parent>
 <artifactId>PROJECT</artifactId>
 <packaging>war</packaging>

 <properties>
 <org.springframework-version>3.1.0.RELEASE</org.springframework-version>
 <spring.batch.version>3.0.0.RELEASE</spring.batch.version>
 </properties>

 <build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <!-- <version>2.3.2</version> -->
 <configuration>
 <source>1.6</source>
 <target>1.6</target>
 <compilerArgument>-Xlint:all</compilerArgument>
 <showWarnings>true</showWarnings>
 <showDeprecation>true</showDeprecation>
 </configuration>
 </plugin>
 <plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>exec-maven-plugin</artifactId>
 <version>1.2.1</version>
 <configuration>
 <mainClass>org.test.int1.Main</mainClass>
 </configuration>
 </plugin>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.16</version>
 <configuration>
 <argLine>-XX:-UseSplitVerifier</argLine>
 </configuration>
 </plugin>
 </plugins>
 </build>

 <dependencies>

 <!-- This dependency (version unspecified) is needed at runtime by Tomcat. 
 I don't know why but I had to add it manually. -->
 <dependency>
 <groupId>asm</groupId>
 <artifactId>asm</artifactId>
 <version>3.3.1</version>
 <scope>runtime</scope>
 </dependency>

 <dependency>
 <groupId>com.sun.jersey</groupId>
 <artifactId>jersey-bundle</artifactId>
 <version>1.10</version>
 </dependency>

 <dependency>
 <groupId>javax.jdo</groupId>
 <artifactId>jdo2-api</artifactId>
 <version>2.3-eb</version>
 </dependency>

 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>${org.springframework-version}</version>
 <exclusions>
 <!-- Exclude Commons Logging in favor of SLF4j -->
 <exclusion>
 <groupId>commons-logging</groupId>
 <artifactId>commons-logging</artifactId>
 </exclusion>
 </exclusions>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context-support</artifactId>
 <version>${org.springframework-version}</version>
 <exclusions>
 <!-- Exclude Commons Logging in favor of SLF4j -->
 <exclusion>
 <groupId>commons-logging</groupId>
 <artifactId>commons-logging</artifactId>
 </exclusion>
 </exclusions>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context-support</artifactId>
 <version>${org.springframework-version}</version>
 <exclusions>
 <!-- Exclude Commons Logging in favor of SLF4j -->
 <exclusion>
 <groupId>commons-logging</groupId>
 <artifactId>commons-logging</artifactId>
 </exclusion>
 </exclusions>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>${org.springframework-version}</version>
 <exclusions>
 <!-- Exclude Commons Logging in favor of SLF4j -->
 <exclusion>
 <groupId>commons-logging</groupId>
 <artifactId>commons-logging</artifactId>
 </exclusion>
 </exclusions>
 </dependency>

 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>${org.springframework-version}</version>
 </dependency>
 </dependencies>
</project>

PropertiesUtil.java


package com.bar.foo.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class PropertiesUtil extends PropertyPlaceholderConfigurer {

 private static Map<String, String> propertiesMap = new HashMap<String, String>();
//Default as in PropertyPlaceholderConfigurer
 private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;

/**
 * Workaround: PropertyPlaceholderConfigurer.systemPropertiesMode is not
 * directly accessible
 */
 @Override
 public void setSystemPropertiesMode(int systemPropertiesMode) {
 super.setSystemPropertiesMode(systemPropertiesMode);
 springSystemPropertiesMode = systemPropertiesMode;
 }

 @Override
 protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) {
 super.processProperties(beanFactory, props);

 for (Object key : props.keySet()) {
 String keyStr = key.toString();
 String valueStr = resolvePlaceholder(keyStr, props, springSystemPropertiesMode);
 propertiesMap.put(keyStr, valueStr);
 }
 }

 public static String getProperty(String name) {
 return propertiesMap.get(name);
 }
}

时间: 作者:

找到了解决方案结果发现我的web.xml 安装错误。 我已经尝试了正确的语法,但是我的tomcat服务器的错误配置导致了错误( NoClassDefFoundError: javax.servlet.ServletException ) 在不同的服务器实例上部署项目的方法是。 正确的配置如下:

添加到 web.xml:


<servlet>
<servlet-name>appServlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>

作者:
...