spring autowire not working没有自动依赖注入类型的限定bean失败;

时间:2014-03-21 17:53:05

标签: java spring-mvc spring-security annotations autowired

每当我尝试运行代码时都会出现此错误。 过去一周我在谷歌寻找答案,但我的班级项目即将到期,我需要一些帮助。我有@service和组件扫描,我实现了服务类。还有什么可能是问题。我检查了所有谷歌。我花了太多天试图调试这个错误无济于事,我的截止日期即将来临。请帮助,非常需要任何帮助。 :)

org.springframework.beans.factory.BeanCreationException: Error creating bean with name'springSecurityFilterChain' defined in ServletContext resource [/WEB-INF/spring/spring-security.xml]: Cannot resolve reference to bean 'userAuthenticationFilter' while setting bean property 'filterChainMap' with key [Root bean: class [org.springframework.security.web.util.AnyRequestMatcher]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'userAuthenticationFilter' defined in ServletContext resource [/WEB-INF/spring/spring-security.xml]: Cannot resolve reference to bean 'customAuthMgr' while setting bean property 'authenticationManager';  nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'customAuthMgr' defined in ServletContext resource [/WEB-INF/spring/spring-security.xml]: Cannot resolve reference to bean 'authenticationProvider' while setting bean property 'providers' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationProvider': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: protected a.b.c.d.projectName.service.UserInfoInterface    a.b.c.d.projectName.springweb.security.UserAuthenticationProvider.userInfoInterface; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [a.b.c.d.projectName.service.UserInfoInterface] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedMap(BeanDefinitionValueResolver.java:379)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:162)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:1681)
at com.ibm.ws.webcontainer.webapp.WebApp.commonInitializationFinish(WebApp.java:374)
at com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:299)
at com.ibm.ws.webcontainer.webapp.WebGroupImpl.addWebApplication(WebGroupImpl.java:100)
at com.ibm.ws.webcontainer.VirtualHostImpl.addWebApplication(VirtualHostImpl.java:166)
at com.ibm.ws.webcontainer.WSWebContainer.addWebApp(WSWebContainer.java:731)
at com.ibm.ws.webcontainer.WSWebContainer.addWebApplication(WSWebContainer.java:616)
at com.ibm.ws.webcontainer.component.WebContainerImpl.install(WebContainerImpl.java:376)
at com.ibm.ws.webcontainer.component.WebContainerImpl.start(WebContainerImpl.java:668)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:1122)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectStart(DeployedApplicationImpl.java:1315)
at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:623)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:940)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:725)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:2046)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:439)
at com.ibm.ws.runtime.component.CompositionUnitImpl.start(CompositionUnitImpl.java:123)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:382)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.access$300(CompositionUnitMgrImpl.java:110)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$CUInitializer.run(CompositionUnitMgrImpl.java:949)
at com.ibm.wsspi.runtime.component.WsComponentImpl$_AsynchInitializer.run(WsComponentImpl.java:349)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1550)    Caused by: org.springframework.beans.factory.BeanCreationException: 

//实体类

package a.b.c.d.projectName.domain.persistence;

import a.b.c.d.projectName.domain.TheRole;
@Entity
@NamedQueries({
    @NamedQuery(name="UserInfo.findByUserID",
            query="select . . ."
    )
})
@Table(name="user_account")
public class UserInfo implements Serializable
{

private static final long serialVersionUID = 1L;

@Id
@Column(name = "userID", length = 20)
private String accountID;   


@Column(name = "userDesc", length = 50)
private String userDesc;

// getters setters  default constructor  toString  compare  

@ManyToMany
@JoinTable(name="userAcct_userRoles",
        joinColumns=@JoinColumn(name="userID"),
        inverseJoinColumns=@JoinColumn(name="userRoleID"))

private Collection<TheRole> theRoles;

public Collection<TheRole> getRoles() {
    return theRoles;
}

public void setRoles(Collection<TheRole> theRoles) {
    this.theRoles = theRoles;
}

}

//接口

package a.b.c.d.projectName.service;

import a.b.c.d.projectName.domain.persistence.UserInfo;

public interface UserInfoInterface {    
public UserInfo getUserInfo(String userID);
}

//服务类

package a.b.c.d.projectName.service.impl;

import  a.b.c.d.projectName.domain.persistence.UserInfo;
import  a.b.c.d.projectName.service.UserInfoInterface;
. . .

@Service  
public class UserInfoImpl implements UserInfoInterface {

@PersistenceContext(unitName = "userInfoPersistenceUnit")
private EntityManager em;

// default constructor...

@Override
public UserInfo getUserInfo(String userID) {

return em.createNamedQuery("UserInfo.findByUserID", UserInfo.class)
        .setParameter("userID", userID).getSingleResult();
}
}

// for spring security

package a.b.c.d.projectName.security;

import a.b.c.d.projectName.domain.persistence.UserInfo;
import a.b.c.d.projectName.domain.persistence.TheRole;
import a.b.c.d.projectName.service.UserInfoInterface;

. . .

public class UserAuthenticationProvider implements AuthenticationProvider {

@Autowired
protected UserInfoInterface userInfoInterface;   // <--spring does not likethis 
. . .     }

// web.xml中

. . .
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:a/b/c/d/springconfig/spring-config.xml
        WEB-INF/spring/spring-security.xml          
    </param-value>
</context-param>

. . .
<servlet>
    <servlet-name>projectNameDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/spring-config.xml</param-value>
    </init-param>       
    <load-on-startup>1</load-on-startup>
</servlet>

  . . .

<filter>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

// spring-config.xml

. . . .
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  

<context:annotation-config />
<aop:aspectj-autoproxy />
<context:component-scan base-package="a.b.c.d.projectName" />    

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="objectMapper" ref="customObjectMapper"/>
            <property name="prefixJson" value="true" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="requireSession" value="false"/> 
</bean>

<bean id="customObjectMapper" class="a.b.c.d.projectName.configuration.JacksonCustomObjectMapper"/> 
<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"/>    

//弹簧security.xml文件

  . . . 
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
    <security:filter-chain-map path-type="ant">
        <security:filter-chain pattern="/**"
            filters=". . .   userAuthFilter,
            . . ." />
    </security:filter-chain-map>
</bean>

<bean id="userAuthFilter" class=". . .">
    <property name="authenticationManager" ref="customAuthMgr" />
</bean>

<bean id="customAuthMgr" class="org.springframework.security.authentication.ProviderManager">
    <property name="providers">
        <list>
            <ref local="userAuthenticationProvider" />
        </list>
    </property>
</bean> 

<bean id="userAuthenticationProvider"
    class="a.b.c.d.projectName.security.UserAuthenticationProvider">
</bean>
     . . . 



Here are the jar files I have

ehcache-core-2.4.6.jar
hibernate-jap-2.0-api-1.0.0.Final.jar
hibernate-validator-4.1.0.Final.jar
hibernate3.jar
jackson-core-asl-1.9.13.jar
jackson-mapper-asl-1.9.13.jar
spring-aop-3.2.4.RELEASE.jar
spring-aspects-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-context-support-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-jdbc-3.2.4.RELEASE.jar
spring-orm-3.2.4.RELEASE.jar
spring-oxm-3.2.4.RELEASE.jar
spring-security-acl-3.2.4.RELEASE.jar
spring-security-aspects-3.2.4.RELEASE.jar
spring-security-config-3.2.4.RELEASE.jar
spring-security-core-3.2.4.RELEASE.jar
spring-security-taglibs-3.2.4.RELEASE.jar
spring-security-web-3.2.4.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
spring-webmvc-3.2.4.RELEASE.jar
spring-xml-3.2.4.RELEASE.jar


here are the xml headers

//Spring-config.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                       http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context-3.2.xsd
                       http://www.springframework.org/schema/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
                       http://www.springframework.org/schema/aop
                       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                       ">

// spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/security  
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">


// web.xml

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

1 个答案:

答案 0 :(得分:0)

将此添加到您的security.xml

 <context:component-scan base-package="a.b.c.d.projectName" />  

为我工作..

祝你好运