将 Spring bean 注入 JSF ManagedBean 时出错

发布于 2024-12-10 21:16:10 字数 2565 浏览 0 评论 0原文

我有一个 JSF ManagedBean,它有一个应该由 Spring 设置的属性。但是,我收到以下错误:

Caused by: javax.el.ELException: java.lang.IllegalArgumentException: Cannot convert persistence.AuthDao@2f6e6ad9 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 persistence.AuthDao@2f6e6ad9 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:添加接口并更改当前的类来实现它们后,我收到以下错误:

部署期间发生错误:加载应用程序时出现异常: java.lang.IllegalStateException:ContainerBase.addChild:开始: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException:错误 创建 ServletContext 资源中定义的名称为“authDao”的 bean [/WEB-INF/applicationContext.xml]:bean初始化失败; 嵌套异常是 org.springframework.beans.ConversionNotSupportedException:失败 转换 '$Proxy157 类型的属性值实现 persistence.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' 属性“userDao”所需的类型“persistence.UserDaoImpl”; 嵌套异常是 java.lang.IllegalStateException:无法转换 类型 [$Proxy157 实现的值 persistence.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] 属性“userDao”所需的类型 [persistence.UserDaoImpl]:否 找到匹配的编辑器或转换策略。请参阅服务器日志 了解更多详情。

I have a JSF ManagedBean which has a property that should be set by Spring. However, I get the following error:

Caused by: javax.el.ELException: java.lang.IllegalArgumentException: Cannot convert persistence.AuthDao@2f6e6ad9 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 persistence.AuthDao@2f6e6ad9 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)

I have the ELresolver in faces-config.xml.

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

It seems that it can find the class, but the class is of another type ($Proxy166?, not sure where that comes from).

PS: Removing the ELResolver seems to do the trick; I thought explicitly providing managed-bean in faces-config.xml would override ELResolver. Is there any way of both of these to coexist, then? Similarly, if I provide both annotation and XML configuration for a bean, which one of these is preferred, or is there a way to merge them, provide some properties in annotation, some in XML?

PPS: After adding interfaces and changing my current classes to implement them, I get the following error:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2024-12-17 21:16:10

这是您班级的代理。您正在实现一个接口,因此 spring 在该接口周围创建一个代理,但您试图通过具体类型注入。改为切换到接口(在托管 bean 中)。

如果出于某种原因确实需要通过具体类注入,可以使用@Scoped(proxyMode=ScopeProxyMode.TARGET_CLASS)

This is a proxy of your class. You are implementing an interface, so spring creates a proxy around the interface, but you are trying to inject by concrete type. Switch to the interface instead (in the managed bean).

If you really need for some reason to inject by concrete class, you can use @Scoped(proxyMode=ScopeProxyMode.TARGET_CLASS)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文