简体   繁体   中英

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:

@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> .

@Autowired works on the bean that are declared explicitly.

If you want to use the @Autowired to set the filterChainDefinitions, then you must declare the it first like below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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