将Springbean注入JSFbean时出现代理异常

将Springbean注入JSFbean时出现代理异常,spring,jsf-2,dependency-injection,code-injection,Spring,Jsf 2,Dependency Injection,Code Injection,我正在尝试将Springbean注入JSFbean,我正在使用Spring3.1和JSF2(Mojarra2.1.7) 我的配置、代码和异常如下所示: StudentService.java: @Scope(proxyMode=ScopedProxyMode.TARGET_CLASS) public class StudentsService extends AbstractMaqraaService { @Override public Set<Class<?&g

我正在尝试将Springbean注入JSFbean,我正在使用Spring3.1和JSF2(Mojarra2.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);
    }

}
@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";
    }
}
<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"/>
...
<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)
模块上下文xml文件中的Springbean:

@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);
    }

}
@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";
    }
}
<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"/>
...
<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服务,请不要忘记为您的服务创建setter:

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

public void setStudentsService (StudentsService studentsService)
{
    this.studentsService = studentsService;
}
对于@Autowired注释,不需要这样做


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

使用
来强制使用JDK代理而不是CGLIB

我不使用Spring,但是您的
studentRegistrationBean
不应该是Spring管理的bean而不是JSF管理的bean,以便能够注入Spring管理的bean吗?至少,这就是JSF依赖项注入和Java EE 6标准CDI依赖项注入的工作方式。@BalusC我正在使用
@ManagedBean
SpringBeanFacesELResolver
进行注入!为什么我不应该那样做!不管怎样,我只是关注了很多关于如何注入的文章和帖子,似乎在所有这些文章中注入都很有效!我只是有个让我头疼的小问题:|