I have web application wich uses jsf 2.0 and spring 3.0 +hibernate 4 The problem is that: jsf managed beans can't use spring beans using dependency injection There are my config files:
Rq When I change Mybean like that @ManagedBean(name="userMB") @RequestScoped and I put @ManagedProperty(value="#{Service}") it is recognized on jsf page but there is error
package com.ardia.service;
import java.util.List;
import com.ardia.model.Composant;
public interface ComposantService {
void inserComposant(Composant comp);
Composant getComposantById(int compId);
Composant getComposant(String compname);
List<Composant> getComposants();
}
this is my service
package com.ardia.service;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ardia.model.Composant;
@Service("compService")
@Transactional
public class ComposantImp implements ComposantService {
@Autowired
SessionFactory sessionFactory;
@Override
public void inserComposant(Composant comp) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().saveOrUpdate(comp);
}
@Override
public Composant getComposantById(int compId) {
// TODO Auto-generated method stub
return (Composant) sessionFactory.
getCurrentSession().
get(Composant.class, compId);
}
@Override
public Composant getComposant(String compname) {
// TODO Auto-generated method stub
return null;
}
@Override
@SuppressWarnings("unchecked")
public List<Composant> getComposants() {
// TODO Auto-generated method stub
Criteria criteria = sessionFactory.
getCurrentSession().
createCriteria(Composant.class);
return criteria.list();
}
}
This is My Managed Bean
package ardia.beans;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.ardia.model.Composant;
import com.ardia.service.ComposantService;
@Component("compBean")
@Scope("session")
public class CoposantBean implements Serializable{
@Autowired
private ComposantService service;
private List<Composant> list;
@PostConstruct
public void init()
{
list=service.getComposants();
}
public List<Composant> getList() {
return list;
}
public void setList(List<Composant> list) {
this.list = list;
}
}
this is My application context spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:component-scan
base-package="ardia.beans" />
<context:component-scan
base-package="com.ardia.service" />
<!-- Data Source Declaration -->
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql:PFE" />
<property name="username" value="postgres" />
<property name="password" value="root" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="packagesToScan" value="com.ardia.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
</beans>
this web.xml and facec.conf.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
faces config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>default</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
and When I try to have a list with datatable .the managedbean it is not recognized
<p:dataTable id="cars" var="fab" value=***"#{here is not figure*** paginator="true" rows="10" >