[英]Spring @Value annotation not working
使用@Value注释的属性未在我的bean上设置。
我的上下文和类位于另一个项目的类路径中的jar内。 它的spring上下文导入我的spring上下文,我的spring上下文从类路径加载一个配置文件,并包含各种bean定义。
懒惰加载没有在任何地方使用,我的jar使用Spring 3.1.4,使用我的jar的项目使用Spring 3.2.3。
当外部项目加载它的上下文(导入我的上下文)时,会加载显示属性文件的记录器
[main] INFO Loading properties file from class path resource [connector-config.properties] - (PropertyPlaceholderConfigurer.java:172:)
摘自我的Spring背景:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
lazy-init="false">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:connector-config.properties</value>
</list>
</property>
</bean>
...
<!-- The actual beans referenced are not foo bar and com but should not be relevant to this issue -->
<bean id="requestGenerator" class="com.connector.RequestGenerator">
<constructor-arg name="foo" ref="foo" />
<constructor-arg name="bar" ref="bar" />
<constructor-arg name="com" ref="com" />
</bean>
使用我的jar在项目的类路径上的配置文件
ruleType=PAUL
response.poolSize=10
ack.poolSize=10
#This needs to be in minutes
max.run.time=100
base.dir=\\
来自外部项目的类从我的上下文加载bean: 通过在Eclipse调试模式中检查requestGen对象,我可以看到属性ruleType为null。 给定上面的属性文件,ruleType应该是“PAUL”但它是null。
public class App
{
public static void main(Straing[] args)
{
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
RequestGenerator requestGen = context.getBean("requestGenerator", RequestGenerator.class);
@Value
注释由AutowiredAnnotationBeanPostProcessor
处理,如果您在XML中具有<component-scan>
或<annotation-config>
配置(或直接使用bean定义),则通常会注册该<annotation-config>
。 您需要添加其中任何一个,可能是<annotation-config>
因为您已经说过没有任何@Component
类。
从Spring 3.1开始,不建议使用PropertyPlaceholderConfigurer
来执行此类任务。 有关更多信息,请参阅其JavaDoc
从另一方面,请显示您的connector-config.properties
和RequestGenerator
的代码,以获取有关问题的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.