Maxiq
Maxiq

Reputation: 133

why does annotate class as @Service do not create bean?

I has class like this:

@Service("userDetailsService") 
public class MyUserDetailsService implements UserDetailsService {
    ...

and trying to do:

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
    </authentication-provider>
</authentication-manager>

and I got followin errors:

Cannot resolve reference to bean 'userDetailsService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userDetailsService' is defined

Is it really necessary to declare bean? In that case like this:

<beans:bean id="myUserDetailsService" class="my.package.services.MyUserDetailsService" />

EDIT

Here is my security.xml file:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/jdbc
           http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <http>
        <form-login login-page="/login/"
            authentication-failure-url="/fail/" />
        <logout logout-success-url="/" />
    </http>

    <context:annotation-config />
    <context:component-scan base-package="my.package" />

    <authentication-manager>
        <authentication-provider user-service-ref="myUserDetailsService">
            <!-- <password-encoder hash="md5" /> -->
        </authentication-provider>
    </authentication-manager>

</beans:beans>

which cause:

The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.

Upvotes: 1

Views: 11959

Answers (4)

Aarambh
Aarambh

Reputation: 61

just import the other xml file in the spring-security.xml by using <beans:import resource="" />

another thing u can do is that load all the xml files in the web.xml file using

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
path to the xml files separated by commas
</param-value>
</context-param>

Upvotes: 1

user926780
user926780

Reputation: 74

You are missing schema location for context.

So your xml should start with:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/jdbc
           http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

Upvotes: 4

ssedano
ssedano

Reputation: 8432

@Service extends @Component which allows for classpath scanning.

You can enable both classpath scanning and annotations

<context:annotation-config />
<context:component-scan base-package="com.package.a,com.b" />

I don't know what version are you using. Try this.

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
        <!-- <password-encoder hash="md5" /> -->
    </authentication-provider>
</authentication-manager>

Unless you provide, as you do, a name, it will be the class name. But you provide the same name it would be but stating another in the config file.

If you @Service with no name then it would be fine.

Upvotes: 1

Lawrence McAlpin
Lawrence McAlpin

Reputation: 2785

If you use annotations to specify your beans, you need to have add an entry to your config to scan the classpath for them.

<context:component-scan base-package="org.example"/>

Upvotes: 4

Related Questions