java org.springframework.beans.TypeMismatchException:无法将类型 [$Proxy133] 的属性值转换为所需类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6177184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:42:11  来源:igfitidea点击:

org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy133] to required type

javaspring

提问by Tapas Bose

In the applicationContext.xml I have a bean ems:

在 applicationContext.xml 我有一个 bean ems

<bean id="ems" class="info.ems.EMSImpl" init-method="init">
    <property name="dao" ref="dao" />
    <property name="passwordEncoder" ref="passwordEncoder" />
    <property name="localeList" value="${ems.locales}" />
    <property name="releaseVersion" value="${ems.version}" />
    <property name="releaseTimestamp" value="${ems.timestamp}" />
    <property name="emsHome" value="${ems.home}" />
</bean>

Now in applicationContext-acegi.xml I am referencing that emsbean:

现在在 applicationContext-acegi.xml 中我引用了那个emsbean:

<bean id="authenticationManager" class="info.ems.config.ProviderManagerFactoryBean">
    <property name="emsImpl" ref="ems"/>        
    <property name="authenticationProvider" ref="authenticationProvider"/>
</bean>

<bean id="authenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
    <property name="userDetailsService" ref="ems"/>
    <property name="passwordEncoder" ref="passwordEncoder"/>
</bean>        

And the ProviderManagerFactoryBean.java:

和 ProviderManagerFactoryBean.java:

public class ProviderManagerFactoryBean implements FactoryBean {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private EMSImpl emsImpl;
    private AuthenticationProvider authenticationProvider;

    public void setEmsImpl(EMSImpl emsImpl) {
        this.emsImpl = emsImpl;
    }

    public void setAuthenticationProvider(AuthenticationProvider authenticationProvider) {
        this.authenticationProvider = authenticationProvider;
    }

            //other code
}

But I am getting this error:

但我收到此错误:

19:08:56,726 INFO  [STDOUT] 2011-05-30 19:08:56,724 [ScannerThread] ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationManager' defined in ServletContext resource [/WEB-INF/applicationContext-acegi.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy133] to required type [info.ems.EMSImpl] for property 'emsImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy133] to required type [info.ems.EMSImpl] for property 'emsImpl': no matching editors or conversion strategy found

The EMSImpl implements EMS.

EMSImpl 实现了EMS。

@Service("emsImpl")
@Transactional(readOnly = true)
public class EMSImpl implements EMS {
     //other code
}

public interface EMS extends UserDetailsService {
     //other code
}

How can I solve this?

我该如何解决这个问题?

Thanks and regards.

谢谢并恭祝安康。

回答by skaffman

When Spring creates the transactional proxy object around EMSImpl, it makes the proxy implement the same set of interfaces at that class (i.e. EMS). The proxy will not, however, be of type EMSImpl.

当 Spring 在 周围创建事务代理对象时EMSImpl,它使代理在该类(即EMS)上实现相同的接口集。但是,代理将不是 类型EMSImpl

In ProviderManagerFactoryBean, you need to inject type type EMS, not EMSImpl. This is also good design - it decouples your classes from each other, so that they communicate via interfaces.

在 中ProviderManagerFactoryBean,您需要注入类型 type EMS,而不是EMSImpl. 这也是一个很好的设计 - 它使您的类彼此分离,以便它们通过接口进行通信。