@Value 在 Spring MVC 3.0.6 中失败
我创建了一个服务并使用 @Service("amazonService") 加载它。 我的应用程序上下文正在使用加载此服务 配置属性
<bean id="propsHolder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/applicationConfig.properties</value>
</list>
</property>
</bean>
我正在使用在我的 Amazon 服务中
public class AmazonServiceImpl implements FileStorageService {
private AmazonS3 amazonClient;
@Value("${abcxyz}")
public String bucketName ;
:并且我的 props 文件有一个属性 abcxyz = my-bucket
但这会严重失败,但出现以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public java.lang.String com.flipswap.service.impl.AmazonServiceImpl.bucketName; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Value(value=${abczyz})}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 50 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Value(value=${abczyz})}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 52 more
Everyone is able to do this successfully and even i succeeded it in a plain java sample. But running in MVC fails.
Any idea?
I have create a Service and loading it using @Service("amazonService").
My application context is loading this service using
I am configuring the properties using
<bean id="propsHolder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/applicationConfig.properties</value>
</list>
</property>
</bean>
In my Amazon service i do:
public class AmazonServiceImpl implements FileStorageService {
private AmazonS3 amazonClient;
@Value("${abcxyz}")
public String bucketName ;
And my props file has a property abcxyz = my-bucket
But this fails badly with the following exception:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public java.lang.String com.flipswap.service.impl.AmazonServiceImpl.bucketName; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Value(value=${abczyz})}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 50 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Value(value=${abczyz})}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 52 more
Everyone is able to do this successfully and even i succeeded it in a plain java sample. But running in MVC fails.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很奇怪。即使该属性因某种原因未加载,
@Value
注释也应解析为括号内的字符串。因此,请尝试升级到 3.1(最近发布),或降级到 3.0.5(我确信这有效)。还要确保您没有更改 xml 中的默认自动装配策略。
This is strange. Even if the property is somehow not loaded, the
@Value
annotation should resolve to the string inside the brackets.So try upgrading to 3.1 (which was recently released), or downgrade to 3.0.5 (where I'm sure this worked). Also make sure you haven't changed the default autowiring strategy in your xml.
反而。
请确保Spring配置文件确实被加载:在其中添加一些语法错误,如果应用程序像以前一样运行,那么问题是该文件没有像预期那样使用。
instead.
Please make sure that the Spring Configuration file is really loaded: add some syntax error in it, if the application run like before, then the problem is that the file is not used like expected.
确保您的 PropertyPlaceholderConfigurer bean 是在与控制器相同的应用程序上下文中声明的。 BeanFactoryPostProcessor(例如 PPC)不会跨父/子应用程序上下文边界继承。您的 PPC 可能是在“根”应用程序上下文中声明的,但您的组件扫描指令是在子(调度程序 servlet)上下文中声明的。这不会起作用,并且会呈现您报告的结果。
Make sure that your PropertyPlaceholderConfigurer bean is declared in the same application context as your controller. BeanFactoryPostProcessors such as PPC are not inherited across parent/child application context boundaries. It may be that your PPC is declared in your 'root' application context, but your component-scanning directive is declared in the child (dispatcher servlet) context. This won't work, and would render the results you've reported.
我得到了它!实际上,该应用程序已在 web.xml 中将其自定义 XMLWebapplication 上下文配置为其 spring 上下文加载器侦听器。
这并没有传递对 super 方法的调用,该方法实际上负责注册 @Value 注释处理器。
I got it! Actually the App had configured its custom XMLWebapplication context in web.xml as its spring context loader listener.
This was not passing in the call to the super method which was actually responsible for registering @Value annotation processors.