2012-05-13 26 views
1

我想要的Spring bean注入到JSF豆,我使用Spring 3.1和JSF 2(鑽嘴魚科2.1.7)代理例外,同時注入的Spring bean爲JSF豆

沒有說很多話我的配置

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 [email protected] 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 [email protected] 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) 

我在Google上搜索了很多,發現很多問題都有我的問題,但沒有任何幫助,我希望我能爲我的特例找到我的解決方案!

+0

我不使用Spring,但應該不是你的'StudentRegistrationMBean'是Spring管理的bean,而不是JSF爲了能夠託管bean注入一個Spring託管bean?至少,這就是JSF依賴注入和Java EE 6標準CDI依賴注入的工作方式。 – BalusC

+0

@BalusC我正在使用'@ ManagedBean'和'SpringBeanFacesELResolver'進行注入!爲什麼我不應該這樣做!無論如何,我只是按照許多文章和職位談論如何注入,似乎在所有這些文章注入工作很好!我只是有一個讓我頭疼的問題:| –

回答

3

使用<aop:aspectj-autoproxy proxy-target-class="true"/>強制使用JDK代理,而不是CGLIB

+0

非常感謝,這是解決我的問題;) –

0

如果你注入你這樣的春季檢修,不要忘記創建的setter爲您服務:

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

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

隨着@Autowired註解沒有必要這麼做。

看看這個answer這是關於不使用接口來使用您的代理。

+0

謝謝,但是,我已經做到了。 –