user2853610
user2853610

Reputation: 33

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?

Upvotes: 0

Views: 503

Answers (1)

Naveen Kumar
Naveen Kumar

Reputation: 963

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>

Upvotes: 1

Related Questions