首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么将类注释为@Service不创建bean?

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

Stack Overflow用户
提问于 2011-11-17 19:22:35
回答 4查看 11.3K关注 0票数 1

我有这样的课:

代码语言:javascript
复制
@Service("userDetailsService") 
public class MyUserDetailsService implements UserDetailsService {
    ...

并试图做到:

代码语言:javascript
复制
<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
    </authentication-provider>
</authentication-manager>

我发现了一些错误:

在设置bean属性'userDetailsService‘时不能解析对bean 'userDetailsService’的引用;嵌套异常为userDetailsService没有定义名为‘userDetailsService’的bean

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

代码语言:javascript
复制
<beans:bean id="myUserDetailsService" class="my.package.services.MyUserDetailsService" />

编辑

这是我的security.xml文件:

代码语言:javascript
复制
<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>

其原因是:

匹配的通配符是严格的,但是不能为元素‘context:注释-config’找到任何声明。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-11-22 18:49:34

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

因此,您的xml应该从以下几个方面开始:

代码语言:javascript
复制
<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">
票数 4
EN

Stack Overflow用户

发布于 2011-11-17 19:27:44

如果使用注释来指定bean,则需要在配置中为它们添加一个条目到扫描类路径

代码语言:javascript
复制
<context:component-scan base-package="org.example"/>
票数 4
EN

Stack Overflow用户

发布于 2011-11-17 19:26:12

@Service扩展了允许classpath scanning@Component

您可以同时启用classpath scanningannotations

代码语言:javascript
复制
<context:annotation-config />
<context:component-scan base-package="com.package.a,com.b" />

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

代码语言:javascript
复制
<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
        <!-- <password-encoder hash="md5" /> -->
    </authentication-provider>
</authentication-manager>

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

如果你没有名字的@Service,那就没问题了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8173009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档