繁体   English   中英

Spring MVC 3.0.6中的@Value失败

[英]@Value failing in 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 ;

我的道具文件具有属性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?

我知道了! 实际上,该应用程序已在web.xml中配置了其自定义XMLWebapplication上下文作为其Spring上下文加载器侦听器。

这没有传递对实际上负责注册@Value注释处理器的super方法的调用。

这很奇怪。 即使未加载该属性, @Value批注也应解析为方括号内的字符串。

因此,请尝试升级到3.1(最近发布),或降级到3.0.5(我确定这是可行的)。 还要确保您没有更改xml中的默认自动装配策略。

<context:property-placeholder location="classpath:/applicationConfig.properties"/>

代替。


请确保确实加载了Spring Configuration文件:在其中添加一些语法错误,如果应用程序像以前那样运行,则问题在于该文件未按预期使用。

确保在与控制器相同的应用程序上下文中声明了PropertyPlaceholderConfigurer bean。 诸如PPC之类的BeanFactoryPostProcessor不会跨父/子应用程序上下文边界继承。 可能是在“根”应用程序上下文中声明了PPC,而在子(调度程序servlet)上下文中声明了组件扫描指令。 这将不起作用,并且会呈现您报告的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM