【问题标题】:why does annotate class as @Service do not create bean?为什么将类注释为@Service 不创建bean?
【发布时间】:2011-11-17 19:22:35
【问题描述】:

我有这样的课:

@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" />

编辑

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

导致:

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

【问题讨论】:

    标签: java spring jakarta-ee ejb


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果您使用注释来指定您的 bean,您需要在配置中为它们添加一个条目到 scan the classpath

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

      【讨论】:

      • 谢谢,我的security.xml文件中有这一行,但没有...但即使添加了它,仍然存在一些问题,请检查编辑
      【解决方案3】:

      @Service 扩展了@Component,它允许classpath scanning

      您可以同时启用classpath scanningannotations

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

      不知道你用的是什么版本。试试这个。

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

      除非你像你一样提供一个名字,否则它将是类名。但是您提供了相同的名称,但在配置文件中声明了另一个名称。

      如果你@Service 没有名字就可以了。

      【讨论】:

      • 谢谢,我有这一行,但不在我的security.xml 文件中...但是即使添加了它,仍然存在一些问题,请检查编辑
      【解决方案4】:

      只需使用&lt;beans:import resource="" /&gt;在spring-security.xml中导入另一个xml文件

      您可以做的另一件事是使用

      加载 web.xml 文件中的所有 xml 文件
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
      path to the xml files separated by commas
      </param-value>
      </context-param>
      

      【讨论】:

        猜你喜欢
        • 2021-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多