0

I am writing a web application using Spring and PrimeFaces. I am trying to inject interface in my managedBean, but when I try to use it, it is null and I get an Exception.

Here is the datasource-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>

and my managedBean class is like this :

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

}

myTestService remains null.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
D.R.
  • 437
  • 2
  • 8
  • 20
  • 3
    AFAIK this problem is because JSF managed beans are handled by JSF bean container that is different from Spring container (where your services class will be handled). There are some blogs on the net that helps you to solve this problem by giving all the job to Spring container. An example from [mkyong](http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/) and from [Spring 3 JSF 2 integration example needed](http://stackoverflow.com/q/7469240/1065197). – Luiggi Mendoza Apr 22 '13 at 15:56
  • Indeed, Luiggi nailed it down. Your bean is managed by JSF, but `@Autowired` works only in Spring managed beans. – BalusC Apr 22 '13 at 15:57
  • yes but what about @ManagedProperty(value = "#{myTestService}") ? this should also kinda inject my class in a managed bean ... – D.R. Apr 22 '13 at 17:04
  • @MrShane please refer to [this answer](http://stackoverflow.com/a/4889226/1065197) to understand the usage of `@ManagedProperty`. – Luiggi Mendoza Apr 22 '13 at 17:07
  • By the way, if you're working on a Java EE 6 web app server environment like JBoss 7 or GlassFish 3, then you could use EJB instead of Spring. – Luiggi Mendoza Apr 22 '13 at 17:08
  • @MrShane, also refer to http://stackoverflow.com/questions/12470512/scoperequest-not-working if you need some background on how everything is tied together. – Ravi Kadaboina Apr 22 '13 at 20:54

1 Answers1

2

I had to add this line in

faces-config.xml

<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
D.R.
  • 437
  • 2
  • 8
  • 20