Spring 无法在托管bean中注入接口
我正在使用Spring和PrimeFaces编写一个web应用程序。我试图在我的managedBean中注入接口,但当我尝试使用它时,它是空的,我得到一个异常 以下是Spring 无法在托管bean中注入接口,spring,jsf,dependency-injection,primefaces,autowired,Spring,Jsf,Dependency Injection,Primefaces,Autowired,我正在使用Spring和PrimeFaces编写一个web应用程序。我试图在我的managedBean中注入接口,但当我尝试使用它时,它是空的,我得到一个异常 以下是数据源config.xml: <context:component-scan base-package="com.myTest"/> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <
数据源config.xml
:
<context:component-scan base-package="com.myTest"/>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/testDS</value>
</property>
</bean>
<bean id="myTestService" class="com.myTest.service.MyTestService">
<property name="dataSource" ref="dataSource"/>
</bean>
java:comp/env/jdbc/testDS
我的managedBean类是这样的:
@ManagedBean(name="myList")
@SessionScoped
public class MyListBean {
private LazyDataModel<Merchant> lazyModel;
@Autowired
@ManagedProperty(value = "#{myTestService}")
private MyTestServiceInterface myTestService;
public MyListBean (){
int a = myTestService.getRecordsCount() ;
}
public LazyDataModel<Merchant> getLazyModel() {
return lazyModel;
}
public void setLazyModel(LazyDataModel<Merchant> lazyModel) {
this.lazyModel= lazyModel;
}
public MyTestServiceInterface getMyTestService() {
return myTestService;
}
public void MyTestServiceInterface (MyTestServiceInterface myTestService) {
this.myTestService= myTestService;
}
@ManagedBean(name=“myList”)
@会议范围
公共类MyListBean{
私人LazyDataModel lazyModel;
@自动连线
@ManagedProperty(value=“#{myTestService}”)
私有MyTestServiceInterface myTestService;
公共MyListBean(){
int a=myTestService.getRecordsCount();
}
公共LazyDataModel getLazyModel(){
返回lazyModel;
}
公共无效setLazyModel(LazyDataModel lazyModel){
this.lazyModel=lazyModel;
}
公共MyTestServiceInterface getMyTestService(){
返回myTestService;
}
公共无效MyTestServiceInterface(MyTestServiceInterface myTestService){
this.myTestService=myTestService;
}
}
myTestService
保持null
我不得不添加这一行
faces-config.xml
org.springframework.web.jsf.el.SpringBeanFacesELResolver
AFAIK这个问题是因为JSF托管bean由JSFbean容器处理,而JSFbean容器不同于Spring容器(您的服务类将在其中处理)。网络上有一些博客通过将所有工作交给SpringContainer来帮助您解决这个问题。一个来自和来自的例子。事实上,Luiggi把它记下来了。您的bean由JSF管理,但是@Autowired
只在Spring管理的bean中工作。是的,但是@ManagedProperty(value=“#{myTestService}”)呢?这也应该将我的类注入到一个托管bean中…@MrShane请参考以了解@ManagedProperty
的用法。顺便说一句,如果您正在使用Java EE 6 web app服务器环境,如JBoss 7或GlassFish 3,那么您可以使用EJB而不是Spring。
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>