代理例外,同时注入弹簧豆到JSF豆(Proxy Exception while injecting

2019-09-17 05:03发布

我想的Spring bean注入到JSF豆,我使用Spring 3.1和JSF 2(钻嘴鱼科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";
    }
}

Spring的bean模块背景下的XML文件:

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

Eception:

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)

我在谷歌做了很多的搜索,发现了很多问题,这里有问题,像我,但没有帮助我,我希望我会找到我对我的特殊情况下的解决方案!

Answer 1:

使用<aop:aspectj-autoproxy proxy-target-class="true"/>执行使用代理JDK的而非CGLIB



Answer 2:

如果你注入你这样的春季服务别忘了为您服务的setter:

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

public void setStudentsService (StudentsService studentsService)
{
    this.studentsService = studentsService;
}

随着@Autowired注解没有必要这么做。

看看这个答案这是关于不使用的接口使用代理。



文章来源: Proxy Exception while injecting spring bean into JSF bean