【问题标题】:Proxy Exception while injecting spring bean into JSF bean将spring bean注入JSF bean时出现代理异常
【发布时间】:2012-05-21 04:04:10
【问题描述】:

我正在尝试将 spring bean 注入 JSF bean,我正在使用 Spring 3.1 和 JSF 2 (Mojarra 2.1.7)

不用多说,我的配置、代码和异常如下所列:

StudentService.java:

@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS)
public class StudentsService extends AbstractMaqraaService {

    @Override
    public Set<Class<?>> getTypes() {
        // TODO Auto-generated method stub
        return null;
    }

    public Student registerStudent(Student student) {
        return this.store(student);
    }

}

StudentRegistrationMBean.java:

@ManagedBean(name="studentRegistrationMBean") 
@SessionScoped
public class StudentRegistrationMBean extends AbstractMBean {

    private Student student;
    @ManagedProperty (value="#{studentsService}")
    private StudentsService studentsService;

    public StudentRegistrationMBean() {
        this.student = new Student();
    }

    /*Setters and getters omitted here only*/

    public String register() {

        studentsService.registerStudent(student);
        return "manageStudents";
    }
}

模块上下文 xml 文件中的 Spring bean:

<bean id="abstractMaqraaService" class="org.tts.maqraa.service.AbstractMaqraaService" abstract="true"/>

<bean id="studentsService" class="org.tts.maqraa.service.StudentsService" lazy-init="default" parent="abstractMaqraaService"/>

faces-config.xml:

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

接收:

TRACE [http-bio-8080-exec-3] (SpringBeanELResolver.java:53) - Successfully resolved variable 'studentsService' in Spring BeanFactory
DEBUG [http-bio-8080-exec-3] (AbstractBeanFactory.java:245) - Returning cached instance of singleton bean 'studentsService'
نوار 13, 2012 11:10:45 ص com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/teacher/registerNewStudent.xhtml]
com.sun.faces.mgbean.ManagedBeanCreationException: Unable to set property studentsService for managed bean studentRegistrationMBean
    at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:615)
    at com.sun.faces.mgbean.ManagedBeanBuilder.buildBean(ManagedBeanBuilder.java:133)
    ...
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.el.ELException: Cannot convert org.tts.maqraa.service.StudentsService@8f65bc0 of type class $Proxy10 to class org.tts.maqraa.service.StudentsService
    at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:420)
    at org.apache.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:47)
    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)
    ... 47 more

ERROR [http-bio-8080-exec-3] (MaqraaExceptionHandler.java:83) - Exception
javax.el.ELException: Cannot convert org.tts.maqraa.service.StudentsService@8f65bc0 of type class $Proxy10 to class org.tts.maqraa.service.StudentsService
    at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:420)
    ...
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我在谷歌上做了很多搜索,发现很多问题都和我一样,但没有任何帮助,我希望我能找到适合我的特殊情况的解决方案!

【问题讨论】:

  • 我不使用 Spring,但您的 StudentRegistrationMBean 不应该是 Spring 托管 bean 而不是 JSF 托管 bean,以便能够注入 Spring 托管 bean?至少,这就是 JSF 依赖注入和 Java EE 6 标准 CDI 依赖注入的工作方式。
  • @BalusC 我正在使用@ManagedBeanSpringBeanFacesELResolver 进行注射!为什么我不应该那样做!无论如何,我只是关注许多关于如何注入的文章和帖子,并且在所有这些文章中似乎注入工作良好!我只是有一个让我头疼的小问题:|

标签: spring jsf-2 dependency-injection code-injection


【解决方案1】:

使用&lt;aop:aspectj-autoproxy proxy-target-class="true"/&gt; 强制使用JDK 代理而不是CGLIB

【讨论】:

    【解决方案2】:

    如果您像这样注入 Spring 服务,请不要忘记为您的服务创建设置器:

    @ManagedProperty (value="#{studentsService}")
    private StudentsService studentsService;
    
    public void setStudentsService (StudentsService studentsService)
    {
        this.studentsService = studentsService;
    }
    

    使用@Autowired 注解就不需要这样做了。

    看看这个answer它是关于不使用接口来使用你的代理的。

    【讨论】:

      猜你喜欢
      • 2011-12-10
      • 2013-06-25
      • 2012-05-12
      • 2011-07-16
      • 1970-01-01
      • 2014-07-01
      • 2011-08-16
      • 2015-04-15
      • 2013-05-21
      相关资源
      最近更新 更多