2011-10-19 80 views
2

我有一个JSF ManagedBean,它有一个应该由Spring设置的属性。但是,我收到以下错误:将Spring bean注入到JSF中时出错ManagedBean

Caused by: javax.el.ELException: java.lang.IllegalArgumentException: Cannot convert [email protected] of type class $Proxy166 to class persistence.AuthDao 
at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:68) 
at com.sun.faces.el.ELUtils.coerce(ELUtils.java:536) 
at com.sun.faces.mgbean.BeanBuilder$Expression.evaluate(BeanBuilder.java:592) 
at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:606) 
... 57 more 
Caused by: java.lang.IllegalArgumentException: Cannot convert [email protected] of type class $Proxy166 to class persistence.AuthDao 
at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:397) 
at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:66) 

我在faces-config.xml中有ELresolver。

<managed-bean> 
    <managed-bean-name>authController</managed-bean-name> 
    <managed-bean-class>controllers.AuthController</managed-bean-class> 
    <managed-bean-scope>session</managed-bean-scope> 
    <managed-property> 
     <property-name>authDao</property-name> 
     <value>#{authDao}</value> 
    </managed-property> 
</managed-bean> 

<application> 
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application> 

它似乎可以找到类,但该类是另一种类型($ Proxy166 ?,不知道从哪里来的)。

PS:删除ELResolver似乎有窍门;我以为明确提供faces-config.xml中的托管bean将覆盖ELResolver。那么这两种方式是否有共存?同样,如果我为bean提供注释和XML配置,哪一个是首选项,或者是否有合并它们的方法,请在注释中提供一些属性,其中一些属性使用XML?

PPS:添加接口和改变我目前的类来实现他们后,我得到以下错误:

Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy157 implementing persistence.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'persistence.UserDaoImpl' for property 'userDao'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy157 implementing persistence.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [persistence.UserDaoImpl] for property 'userDao': no matching editors or conversion strategy found. Please see server.log for more details.

回答

7

这是你的类的代理。你正在实现一个接口,所以Spring会在接口周围创建一个代理,但是你正在尝试按具体类型注入。改为切换到界面(在托管bean中)。

如果你真的需要出于某种原因由具体类注入时,可以使用@Scoped(proxyMode=ScopeProxyMode.TARGET_CLASS)

+0

其实,我并没有落实在这种情况下,一个接口,这是一个快速演示。那么,Spring是否迫使我默认使用接口? – ustun

+0

没有。但是你的bean肯定有一个接口,否则你不会得到一个$ ProxyXX。如果没有界面,它会使用cglib – Bozho

+0

感谢您的帮助。虽然我的项目中没有接口,但只有类。添加接口也没有解决问题,我在上面添加了错误信息。 – ustun

相关问题