gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:56:54 30 4
gpt4 key购买 nike

我有这样的类(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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com