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

标签 java spring jakarta-ee ejb

我有这样的类(class):

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

并努力做到:

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

我遇到了以下错误:

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

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

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

编辑

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

原因:

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

最佳答案

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

所以你的 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">

关于java - 为什么将类注释为@Service 不创建 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8173009/

相关文章:

java - 仅使用 for 循环删除 ArrayList 中的重复项

java - 如何将类中的对象存储在数组中

java - 单个 Spring @Transaction 中的两个 dao 方法

spring - java.lang.NoSuchMethodError : org. springframework.beans.factory.xml.BeanDefinitionParserDelegate.getLocalName(Lorg/w3c/dom/Node;)Ljava/lang/String;

java - spring jdbc 模板返回空结果

java - Android更改语言配置会弄乱布局

java - 为什么要对 spring bean 进行两次初始化

java - PowerMockito 模拟单个静态方法并返回对象

java - Java EE 7 属性文件配置的最佳实践建议是什么?

java - Spring Boot 应用程序。 SecurityContextHolder 与 HttpSession