为什么将类注释为@Service不创建bean?

2023-11-29

我有这样的课程:

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

并尝试做:

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

我收到以下错误:

设置时无法解析对 bean 'userDetailsS​​ervice' 的引用 bean 属性“userDetailsS​​ervice”;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 定义了名为“userDetailsS​​ervice”的 bean

真的有必要声明bean吗?在这种情况下像这样:

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

EDIT

这是我的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>

哪个原因:

匹配通配符严格,但找不到声明 元素“上下文:注释配置”。


您缺少上下文的架构位置。

所以你的 xml 应该以以下内容开头:

<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">
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么将类注释为@Service不创建bean? 的相关文章

随机推荐