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

时间:2011-05-30 13:52:25

标签: java spring

在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>

现在在applicationContext-acegi.xml中我引用了ems bean:

<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>        

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
}

但是我收到了这个错误:

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

EMSImpl实施EMS。

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

public interface EMS extends UserDetailsService {
     //other code
}

我该如何解决这个问题?

谢谢和问候。

1 个答案:

答案 0 :(得分:5)

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

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