Java Spring @Value 注释不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22672263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring @Value annotation not working
提问by AfterWorkGuinness
Properties annotated with @Value are not being set on my beans.
没有在我的 bean 上设置用 @Value 注释的属性。
My context and classes are inside a jar that is on the classpath of another project. Its spring context imports my spring context and my spring context loads a config file from the classpath and contains various bean definitions.
我的上下文和类位于另一个项目的类路径上的 jar 中。它的 spring 上下文导入我的 spring 上下文,我的 spring 上下文从类路径加载一个配置文件,并包含各种 bean 定义。
Lazy loading is not being used anywhere, my jar is using Spring 3.1.4 and the project using my jar is using Spring 3.2.3.
延迟加载未在任何地方使用,我的 jar 使用 Spring 3.1.4,使用我的 jar 的项目使用 Spring 3.2.3。
Logger showing properties file is loaded when the external project loads it's context (which imports mine)
当外部项目加载其上下文(导入我的)时,会加载显示属性文件的记录器
[main] INFO Loading properties file from class path resource [connector-config.properties] - (PropertyPlaceholderConfigurer.java:172:)
Excerpt from my Spring context:
摘自我的 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>
config file that is on classpath of project using my jar
使用我的 jar 的项目类路径上的配置文件
ruleType=PAUL
response.poolSize=10
ack.poolSize=10
#This needs to be in minutes
max.run.time=100
base.dir=\
Class from external project loading a bean from my context: By inspecting the requestGen object in Eclipse debug mode I can see the property ruleType is null.Given the above properties file, ruleType should be "PAUL" but it is null.
来自外部项目的类从我的上下文加载 bean: 通过在 Eclipse 调试模式下检查 requestGen 对象,我可以看到属性 ruleType 为 null。鉴于上述属性文件,ruleType 应为“PAUL”但为空。
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);
采纳答案by Sotirios Delimanolis
The @Value
annotation is processed by AutowiredAnnotationBeanPostProcessor
which is typically registered if you have a <component-scan>
or <annotation-config>
configuration in XML (or directly with a bean definition). You need to add either of those, probably <annotation-config>
since you've said you don't have any @Component
classes.
该@Value
注释被处理由AutowiredAnnotationBeanPostProcessor
哪个如果你有一个通常注册<component-scan>
或<annotation-config>
在XML配置(或直接与bean定义)。您需要添加其中任何一个,可能是<annotation-config>
因为您说过您没有任何@Component
课程。
回答by Artem Bilan
Since Spring 3.1 it isn't recommended to use PropertyPlaceholderConfigurer
for such task. For more info see its JavaDoc
从 Spring 3.1 开始,不建议将其PropertyPlaceholderConfigurer
用于此类任务。有关更多信息,请参阅其JavaDoc
From other side, show, please, your connector-config.properties
and the code of RequestGenerator
to have more info about an issue.
从另一方面,请显示您的connector-config.properties
和代码RequestGenerator
以获取有关问题的更多信息。