【发布时间】:2017-10-03 14:28:26
【问题描述】:
我有一个 Spring Web 应用程序(RestFul Web 服务)
我的applicationContext.xml 中有以下内容可以读取属性文件:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="appProperties" />
</bean>
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:common.properties</value>
<value>classpath:local.properties</value>
<value>classpath:${XPLAT_ENV}.properties</value>
</list>
</property>
</bean>
而我的 Rest API 是这样的:
@Path("/login")
@Component
public class CLSAmple {
@Autowired
@Qualifier("appProperties")
protected Properties appProperties;
@Value("${ui.server.endpoint}")
private String uiservername;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getLoginInformation() {
if(appProperties != null){
System.out.println("AppProperties is NOT null");
String uiServer = appProperties.getProperty("ui.server.endpoint");
return uiServer;
}
System.out.println("AppProperties is NULL");
System.out.println("@Value is " + uiservername);
return "Hello Java!!";
}
}
但我的@Autowired appProperties 和@Value 获取特定属性始终是NULL。我查看了关于从属性文件中注入值的类似帖子,并尝试了所有可能的建议,但它仍然是NULL。
还尝试了仅使用注释的方法
@PropertySource("classpath:common.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:metrics="http://www.ryantenney.com/schema/metrics"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.ryantenney.com/schema/metrics
http://www.ryantenney.com/schema/metrics/metrics.xsd">
<context:component-scan base-package="com.cl.connect" />
<context:annotation-config/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="appProperties" />
</bean>
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:common.properties</value>
</list>
</property>
</bean>
</beans>
【问题讨论】:
-
编辑添加:我的 common.properties 在 src/main/resources
-
你能不能把你的整个 applicationContext.xml
-
JAX WS 将您的类与 Spring 分开实例化。您需要配置集成,以便它从您的 Spring 上下文中检索它们。
-
既然已经使用了spring,为什么还要使用jax ws?只需使用spring restControllers,如果使用spring,您不必手动配置属性
-
@Plog 添加了 applicationContext.xml 我需要使用 jax ws,因为 tis 是现有项目的模块,需要遵循其他项目使用的内容,以保持一致性
标签: java spring rest web-services