【问题标题】:Spring dependency issue - no matching editors or conversion strategy foundSpring 依赖问题 - 找不到匹配的编辑器或转换策略
【发布时间】:2014-10-02 00:59:20
【问题描述】:

我有一个 Web 应用程序在部署时失败。我收到以下错误:

Aug 8, 2014 7:00:21 PM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#2' while setting bean property 'sourceList' with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#2': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'CustomLoginSuccessHandler' while setting bean property 'authenticationSuccessHandler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomLoginSuccessHandler': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.abc.xyz.ldapservices.LDAPUtil com.abc.xyz.web.CustomLoginSuccessHandler.ldapContact; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapContact' defined in ServletContext resource [/WEB-INF/spring/spring-security.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy213 implementing org.springframework.ldap.core.LdapOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'org.springframework.ldap.core.LdapTemplate' for property 'ldapTemplate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy213 implementing org.springframework.ldap.core.LdapOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.ldap.core.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
    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.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1391)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    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:589)
    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:383)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:661)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
    at weblogic.servlet.internal.EventsManager.executeContextListener(EventsManager.java:241)
    at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:198)
    at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:183)
    at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1783)
    at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:2807)
    at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1661)
    at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:822)
    at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:360)
    at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:356)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42)
    at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:138)
    at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:213)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:208)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42)
    at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:70)
    at weblogic.application.internal.flow.StartModulesFlow.activate(StartModulesFlow.java:24)
    at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:729)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42)
    at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:258)
    at weblogic.application.internal.SingleModuleDeployment.activate(SingleModuleDeployment.java:48)
    at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165)
    at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:80)
    at weblogic.deploy.internal.targetserver.operations.AbstractOperation.activate(AbstractOperation.java:586)
    at weblogic.deploy.internal.targetserver.operations.ActivateOperation.activateDeployment(ActivateOperation.java:148)
    at weblogic.deploy.internal.targetserver.operations.ActivateOperation.doCommit(ActivateOperation.java:114)
    at weblogic.deploy.internal.targetserver.operations.AbstractOperation.commit(AbstractOperation.java:339)
    at weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentCommit(DeploymentManager.java:846)
    at weblogic.deploy.internal.targetserver.DeploymentManager.activateDeploymentList(DeploymentManager.java:1275)
    at weblogic.deploy.internal.targetserver.DeploymentManager.handleCommit(DeploymentManager.java:442)
    at weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.commit(DeploymentServiceDispatcher.java:176)
    at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doCommitCallback(DeploymentReceiverCallbackDeliverer.java:195)
    at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access$100(DeploymentReceiverCallbackDeliverer.java:13)
    at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer$2.run(DeploymentReceiverCallbackDeliverer.java:68)
    at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:550)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:295)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:254)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#2': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'CustomLoginSuccessHandler' while setting bean property 'authenticationSuccessHandler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomLoginSuccessHandler': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.abc.xyz.ldapservices.LDAPUtil com.abc.xyz.web.CustomLoginSuccessHandler.ldapContact; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapContact' defined in ServletContext resource [/WEB-INF/spring/spring-security.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy213 implementing org.springframework.ldap.core.LdapOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'org.springframework.ldap.core.LdapTemplate' for property 'ldapTemplate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy213 implementing org.springframework.ldap.core.LdapOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.ldap.core.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
    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.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:615)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)

我正在使用 web.xml 加载我的配置 XML:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/spring/spring-security.xml
            WEB-INF/spring/applicationContext-db.xml
            WEB-INF/spring/applicationContext.xml
        </param-value>
    </context-param>

错误来自我的 spring-security.xml 文件,该文件有:

<security:http auto-config="true" use-expressions="true"
        access-denied-page="/auth/denied">
        <security:intercept-url pattern="/auth/login"
            access="permitAll" />
        <security:intercept-url pattern="/main/admin"
            access="hasRole('ROLE_ADMIN')" />
        <security:intercept-url pattern="/main/common"
            access="hasRole('ROLE_RI')" />

        <security:form-login login-page="/auth/login"
            authentication-failure-url="/auth/login?error=true"
            default-target-url="/main/common" authentication-success-handler-ref="CustomLoginSuccessHandler" />


        <security:logout invalidate-session="true" logout-url="/auth/logout"
            success-handler-ref="CustomLogoutSuccessHandler" />

    </security:http>

    <bean id="CustomLoginSuccessHandler"
        class="com.abc.xyz.web.CustomLoginSuccessHandler">
        <property name="ldapContact" ref="ldapContact"></property>
    </bean>
    <bean id="CustomLogoutSuccessHandler"
        class="com.abc.xyz.web.CustomLogoutSuccessHandler">
    </bean>

    <security:authentication-manager>
        <security:ldap-authentication-provider
            user-search-filter="(uid={0})" user-search-base="ou=Users"
            group-search-filter="(roleOccupant={0})" group-search-base="ou=Groups"
            group-role-attribute="cn" role-prefix="ROLE_">
        </security:ldap-authentication-provider>
    </security:authentication-manager>

    <security:ldap-server url="ldap://localhost:389/dc=maxcrc,dc=com"
        manager-dn="cn=Manager,dc=maxcrc,dc=com" manager-password="secret" />

    <security:global-method-security
        secured-annotations="enabled" />

<!--    <bean id="placeHolderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="location" value="/WEB-INF/classes/ldap.properties"></property>
    </bean> 
  <context:property-placeholder location="classpath:WEB-INF/classes/ldap.properties"/>
-->
    <bean id="contextSource"
        class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
        <constructor-arg value="ldap://localhost:389/dc=maxcrc,dc=com" />
        <property name="userDn" value="cn=Manager,dc=maxcrc,dc=com" />
        <property name="password" value="secret" />
    </bean>

    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
        <constructor-arg ref="contextSource" />
        <property name="ignorePartialResultException" value="true" />
    </bean>
    <bean id="ldapContact" class="com.abc.xyz.ldapservices.LDAPUtil">
        <property name="ldapTemplate" ref="ldapTemplate" />
    </bean>

我的 CustomLoginSuccessHandler 类是这样的:

public class CustomLoginSuccessHandler implements AuthenticationSuccessHandler {


    /** The redirect strategy. */
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    /** The ldap contact. */
    @Autowired
    LDAPUtil ldapContact;

我是 spring 新手,但我想这是由于 bean 依赖项配置。

1. spring bean依赖的顺序是什么,有没有用到 来自 web.xml 的 XML 配置序列?
2.假设我指的是bean A 在一个 XML 文件中,它在下面被引用的地方定义,它会 有什么问题吗?

编辑

我的 LDAPUtil 类是这样的:

private LdapOperations ldapTemplate;

public void setLdapTemplate(LdapTemplate ldapTemplate) {
            this.ldapTemplate = ldapTemplate;
}

【问题讨论】:

    标签: java spring spring-security spring-ioc


    【解决方案1】:

    错误提供了有用的元素。原因(链中的最后一个错误)是(剥离字符串):

    创建名为“ldapContact”的 bean 时出错...无法将类型“$Proxy213 implementation ... LdapOperations ...”的属性值转换为属性“ldapTemplate”所需的类型“....LdapTemplate”

    您没有显示LdapUtil 源,但来自CustomSuccessHandler 和错误,我认为它包含某处:

    @Autowired
    LdapTemplate ldapTemplate;
    

    该错误表明ldapTemplatebean 周围存在AOP 代理机制。

    这不是问题,除了默认情况下 Spring AOP 使用 JDK 代理,代理实现所有接口,但不扩展原始类。您应该可以通过接口引用ldapTemplate 来修复错误:

    @Autowired
    LdapOperations ldapTemplate;
    

    或者,您可以要求 Spring 使用 cglib 代理,通过在 xml 文件中使用 &lt;aop:config proxy-target-class = "true"&gt;(如果您使用注释,则使用 &lt;aop:aspectj-autoproxy proxy-target-class="true"/&gt;)扩展原始类。但这需要 cglib jars 在路径上,并且您可以使用接口,我建议您使用第一个解决方案。

    编辑:

    您编辑的帖子显示了您如何在LdapUtil 中加载ldapTemplate。当你应该以这种方式使用接口时,你将它作为一个类加载:

    private LdapOperations ldapTemplate;
    
    public void setLdapTemplate(LdapOperations ldapTemplate) {
                this.ldapTemplate = ldapTemplate;
    }
    

    【讨论】:

    • 尝试了你建议的界面方式,但它仍然给我同样的错误,我也尝试将 org.springframework.ldap.core.LdapTemplate 更改为 org.springframework.ldap.core.LdapOperations xml,但它给了我构造函数异常。
    • 如果您在 LdapUtil.java 中写入 @Autowired LdapOperations ldapTemplate; 并进行干净的重建,则不会出现相同的错误。也许是另一个,但不是 Failed to convert ... to required type '....LdapTemplate' for bean ldapContact
    • 我没有为 ldapTemplate 提供 @Autowired 注释,因为我在我的 spring-security.xml 文件本身中为 LdapUtil.java 定义了 bean。
    猜你喜欢
    • 2011-09-21
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多