Hibernate NoSuchBeanDefinitionException:未找到依赖项类型的符合条件的bean

Hibernate NoSuchBeanDefinitionException:未找到依赖项类型的符合条件的bean,hibernate,spring-mvc,authentication,Hibernate,Spring Mvc,Authentication,当我尝试在Eclipse中运行该项目时,出现以下错误。我尝试了类似问题的不同解决方案,但无法找出确切的问题 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.filterChains”的bean时出错:使用键[1]设置bean属性“sourceList”时,无法解析对bean“org.springframework.security.web.DefaultSecuri

当我尝试在Eclipse中运行该项目时,出现以下错误。我尝试了类似问题的不同解决方案,但无法找出确切的问题

org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.filterChains”的bean时出错:使用键[1]设置bean属性“sourceList”时,无法解析对bean“org.springframework.security.web.DefaultSecurityFilterChain”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.web.DefaultSecurityFilterChain#1”的bean时出错:使用键[4]设置构造函数参数时无法解析对bean“org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter”的bean时出错:设置bean属性“authenticationManager”时无法解析对bean“org.springframework.security.authentication.ProviderManager”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authentication.ProviderManager#0”的bean时出错:设置构造函数参数时无法解析对bean“org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0”的bean时出错:FactoryBean在创建对象时引发异常;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authenticationManager”的bean时出错:使用键[0]设置构造函数参数时无法解析对bean“authService”的引用;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“authService”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连接字段:com.demo.dao.UserDAO com.demo.service.impl.AuthServiceImpl.UserDAO;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[com.demo.dao.UserDAO]的符合条件的bean:应至少有1个bean符合此依赖项的autowire候选项的条件。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 位于org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)

我的
security.xml
是:

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>

<http pattern="/resources/**" security="none"/>

<http access-decision-manager-ref="accessDecisionManager" use-expressions="true">
    <intercept-url pattern="/forgotPasswordJson" access="permitAll" />
    <intercept-url pattern="/resetPassword" access="permitAll" />
    <intercept-url pattern="/login" access="permitAll" />
    <intercept-url pattern="/logout" access="permitAll" />
    <intercept-url pattern="/loginfailed" access="permitAll" />
    <intercept-url pattern="/setTimezoneOffset" access="permitAll" />
    <intercept-url pattern="/images/**" access="permitAll" />
    <intercept-url pattern="/info/termsOfService" access="permitAll" />
    <intercept-url pattern="/**" access="hasRole('USER')" />
    <form-login login-page='/login' authentication-failure-url="/loginfailed" authentication-success-handler-ref="customAuthenticationHandler" />
    <logout delete-cookies="JSESSIONID" logout-success-url="/login" logout-url="/logout" invalidate-session="true"/>
    <session-management session-authentication-error-url="/loginfailed" session-fixation-protection="newSession">
        <concurrency-control max-sessions="100" error-if-maximum-exceeded="true" />
    </session-management>
</http>

<authentication-manager>
    <authentication-provider ref="authService" />
</authentication-manager>
<beans:bean id="authService" class="com.demo.service.impl.AuthServiceImpl" />
<beans:bean id="customAuthenticationHandler" class="com.demo.security.CustomAuthenticationSuccessHandler" />
@SuppressWarnings("unchecked")
@Override
public User findUser(String userName, boolean allData)
{
    if (logger.isDebugEnabled()) logger.debug("Finding user by user name \"" + userName + "\"");

    List<User> list = entityManager.createQuery("from User u where upper(u.userName) = :name")
            .setParameter("name", userName.toUpperCase()).getResultList();

    if (list.size() != 1) return null;
    User u = list.get(0);

    if (u != null && allData)
    {
        Hibernate.initialize(u.getClients());
        Hibernate.initialize(u.getRoles());
    }

    return u;
}
@Service
public class AuthServiceImpl implements AuthSerice, AuthenticationProvider {

@Autowired
UserDAO userDao;
@Override
public Authentication authenticate(Authentication authentication) 
  throws AuthenticationException {
    System.out.println("Entered Java auth");
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();

    User details = userDao.findUser(name, true);
    Collection<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
    SimpleGrantedAuthority userAuthority = new SimpleGrantedAuthority(
            "ROLE_USER");
    SimpleGrantedAuthority adminAuthority = new SimpleGrantedAuthority(
            "ROLE_ADMIN");
    authorities.add(userAuthority);
    authorities.add(adminAuthority);

    if (password.equals(details.getPassword())) {
        return new UsernamePasswordAuthenticationToken(name, password, authorities);
    } else {
        //throw new AuthenticationException("Unable to auth against third party systems");
        return null;
    }
}

@Override
public boolean supports(Class<?> authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
我的
AuthServiceImpl
是:

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>

<http pattern="/resources/**" security="none"/>

<http access-decision-manager-ref="accessDecisionManager" use-expressions="true">
    <intercept-url pattern="/forgotPasswordJson" access="permitAll" />
    <intercept-url pattern="/resetPassword" access="permitAll" />
    <intercept-url pattern="/login" access="permitAll" />
    <intercept-url pattern="/logout" access="permitAll" />
    <intercept-url pattern="/loginfailed" access="permitAll" />
    <intercept-url pattern="/setTimezoneOffset" access="permitAll" />
    <intercept-url pattern="/images/**" access="permitAll" />
    <intercept-url pattern="/info/termsOfService" access="permitAll" />
    <intercept-url pattern="/**" access="hasRole('USER')" />
    <form-login login-page='/login' authentication-failure-url="/loginfailed" authentication-success-handler-ref="customAuthenticationHandler" />
    <logout delete-cookies="JSESSIONID" logout-success-url="/login" logout-url="/logout" invalidate-session="true"/>
    <session-management session-authentication-error-url="/loginfailed" session-fixation-protection="newSession">
        <concurrency-control max-sessions="100" error-if-maximum-exceeded="true" />
    </session-management>
</http>

<authentication-manager>
    <authentication-provider ref="authService" />
</authentication-manager>
<beans:bean id="authService" class="com.demo.service.impl.AuthServiceImpl" />
<beans:bean id="customAuthenticationHandler" class="com.demo.security.CustomAuthenticationSuccessHandler" />
@SuppressWarnings("unchecked")
@Override
public User findUser(String userName, boolean allData)
{
    if (logger.isDebugEnabled()) logger.debug("Finding user by user name \"" + userName + "\"");

    List<User> list = entityManager.createQuery("from User u where upper(u.userName) = :name")
            .setParameter("name", userName.toUpperCase()).getResultList();

    if (list.size() != 1) return null;
    User u = list.get(0);

    if (u != null && allData)
    {
        Hibernate.initialize(u.getClients());
        Hibernate.initialize(u.getRoles());
    }

    return u;
}
@Service
public class AuthServiceImpl implements AuthSerice, AuthenticationProvider {

@Autowired
UserDAO userDao;
@Override
public Authentication authenticate(Authentication authentication) 
  throws AuthenticationException {
    System.out.println("Entered Java auth");
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();

    User details = userDao.findUser(name, true);
    Collection<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
    SimpleGrantedAuthority userAuthority = new SimpleGrantedAuthority(
            "ROLE_USER");
    SimpleGrantedAuthority adminAuthority = new SimpleGrantedAuthority(
            "ROLE_ADMIN");
    authorities.add(userAuthority);
    authorities.add(adminAuthority);

    if (password.equals(details.getPassword())) {
        return new UsernamePasswordAuthenticationToken(name, password, authorities);
    } else {
        //throw new AuthenticationException("Unable to auth against third party systems");
        return null;
    }
}

@Override
public boolean supports(Class<?> authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
@服务
公共类AuthServiceImpl实现AuthSerice,AuthenticationProvider{
@自动连线
UserDAO UserDAO;
@凌驾
公共身份验证(身份验证)
抛出AuthenticationException{
System.out.println(“输入JavaAuth”);
String name=authentication.getName();
字符串密码=authentication.getCredentials().toString();
用户详细信息=userDao.findUser(名称,true);
收集权限=新建ArrayList();
SimpleGrantedAuthority userAuthority=新的SimpleGrantedAuthority(
“角色用户”);
SimpleGrantedAuthority adminAuthority=新SimpleGrantedAuthority(
“角色管理”);
权限。添加(用户权限);
权限。添加(adminAuthority);
if(password.equals(details.getPassword())){
返回新的用户名PasswordAuthenticationToken(名称、密码、权限);
}否则{
//抛出新的AuthenticationException(“无法针对第三方系统进行身份验证”);
返回null;
}
}
@凌驾
公共布尔支持(类身份验证){
返回authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
我的web.xml是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" 
     version="2.5"
     metadata-complete="true">

<display-name>MainWebsite</display-name>

<session-config>
    <session-timeout>120</session-timeout>
</session-config>

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml
                 /WEB-INF/spring/security.xml</param-value>
</context-param>

<!-- Filters -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<jsp-config>
    <taglib>
        <taglib-uri>/spring</taglib-uri>
        <taglib-location>/WEB-INF/tld/spring-form.tld</taglib-location>
    </taglib>
</jsp-config>
</web-app>

主网站
120
上下文配置位置
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/security.xml
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
org.springframework.web.context.ContextLoaderListener
org.springframework.security.web.session.HttpSessionEventPublisher
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/
/弹簧
/WEB-INF/tld/spring-form.tld
这里,我尝试从Java类对用户进行身份验证,而不是使用XML中的查询进行身份验证

我的
context:component scan
位于
servlet context.xml
中。如果我在
security.xml
中添加
,则错误正在更改。我认为错误可能是因为xml加载优先级


p.S.:错误的修复方法是将
servlet context.xml
移动到
WEB-INF
文件夹,相应地重命名它,然后将其添加/声明到
WEB.xml
以及其他xml文件中。

com.vigillo.mainwebsiteroot
包配置了类路径扫描,但您的类似乎位于
com.demo
包中

看来您的意图是明确地声明
com.demo

定义了authService和customAuthenticationHandler bean,但上下文中没有userDao。这可能就是问题所在

定义它,看看会发生什么

<beans:bean id="userDao" class="com.demo...UseDaoImpl"/>

com.vigillo.mainwebsiteroot
包配置了类路径扫描,但您的类似乎位于
com.demo
包中

看来是你的意图