简体   繁体   English

春季使用自动装配注释会出错

[英]Spring use Autowired annotations gets wrong

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">   
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="/"/>
    <property name="filterChainDefinitionMap" ref="chainFilterBuff" />
</bean> 

<bean id="chainFilterBuff"   class="org.moofie.test.security.FilterChainBean">
    <property name="filterChainDefinitions">
        <value>/test/login=anon</value>
    </property>
</bean>

above is my spring config 以上是我的春季配置

private String filterChainDefinitions;
public String getFilterChainDefinitions() {
    return filterChainDefinitions;
}

public void setFilterChainDefinitions(String filterChainDefinitions) {
    this.filterChainDefinitions = filterChainDefinitions;
}

and this is my java code,it works fine with getter and setter,but I want to replace getter and setter with @autowired annotaion like this: 这是我的java代码,它可以与getter和setter一起正常工作,但是我想用@autowired注释替换getter和setter,如下所示:

@Autowired
private String filterChainDefinitions;

it gets errors: 它得到错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying 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.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 34 more

So whats wrong with my code?or I should use other annotations? 那么我的代码有什么问题呢?还是我应该使用其他注释?

The setter and getter way is working because, in your config XML you are calling the setter directly using the <property name="filterChainDefinitions"></property> . setter和getter方法之所以有效,是因为在配置XML中,您直接使用<property name="filterChainDefinitions"></property>调用setter。

@Autowired works on the bean that are declared explicitly. @Autowired在显式声明的bean上工作。

If you want to use the @Autowired to set the filterChainDefinitions, then you must declare the it first like below: 如果要使用@Autowired设置filterChainDefinitions,则必须首先声明它,如下所示:

<bean id="filterChainDefinitions" class="java.lang.String">
    <constructor-arg value="/test/login=anon"/>
</bean>

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

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