integration jsf,spring,hibernate: can't call bean in jsf file - spring

I'm working on a project using maven,hibernate,spring and jsf(primefaces). I have a problem with the beans. In the xhtml file i can't call the bean , it doesn't recognise it. for example :value="#{collaborateurBean.listeCollaborateurs}. when i try to call it, it doesn't work. Here i just wrote even though it doesn't appear.
It seems like everything that i did is good but i don't know what i'm missing.
when i run collaborateur.xhtml i get an empty datatable.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ma.ensa</groupId>
<artifactId>Projet_sqli</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Projet_sqli Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.8-02</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.8-02</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>start</artifactId>
<version>1.11.2</version>
</dependency>
</dependencies>
<build>
<finalName>Projet_sqli</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</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>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
faces-config.xml
<?xml version="1.0"?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="view">
<bean class="org.primefaces.spring.scope.ViewScope"/>
</entry>
</map>
</property>
</bean>
<context:annotation-config/>
<context:component-scan base-package="dao"/>
<context:component-scan base-package="model"/>
<context:component-scan base-package="controller"/>
</beans>
collaborateur.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body style="background-color: #ECF3FD;">
<h:form id="form">
<p:dataTable id="display" var="listeCollaborateurs" value="#{collaborateurBean.listeCollaborateurs}" editable="true" style="margin-bottom:10px">
<f:facet name="header">
Liste des collaborateurs
</f:facet>
<p:column headerText="Id Collaborateur">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{listeCollaborateurs.idCollaborateur}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{listeCollaborateurs.idCollaborateur}" style="width:100%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Raison sociale">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{listeCollaborateurs.cin}" /></f:facet>
<f:facet name="input"><p:inputText value="#{listeCollaborateurs.cin}" style="width:100%" label="Year"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="RIB">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{listeCollaborateurs.nom}" /></f:facet>
<f:facet name="input"><p:inputText value="#{listeCollaborateurs.nom}" style="width:100%" label="Year"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Adresse">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{listeCollaborateurs.prenom}" /></f:facet>
<f:facet name="input"><p:inputText value="#{listeCollaborateurs.prenom}" style="width:100%" label="Year"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Date de naissance">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{listeCollaborateurs.dateNaissance}" /></f:facet>
<f:facet name="input"><p:inputText value="#{listeCollaborateurs.dateNaissance}" style="width:100%" label="Year"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Téléphone">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{listeCollaborateurs.telephone}" /></f:facet>
<f:facet name="input"><p:inputText value="#{listeCollaborateurs.telephone}" style="width:100%" label="Year"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Email">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{listeCollaborateurs.email}" /></f:facet>
<f:facet name="input"><p:inputText value="#{listeCollaborateurs.email}" style="width:100%" label="Year"/></f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
CollaborateurBean.java
package controller;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import model.CollaborateurMetier;
import net.vo.Collaborateur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
#Component
#Scope("view")
public class CollaborateurBean implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#Autowired
private CollaborateurMetier collaborateurMetier;
private List<Collaborateur> listeCollaborateurs;
private int idCollaborateur;
private String cin;
private String nom;
private String prenom;
private Date dateNaissance;
private String telephone;
private String email;
#PostConstruct
public void init() {
listeCollaborateurs = collaborateurMetier.getAllCollaborateurs();
}
public CollaborateurMetier getCollaborateurMetier() {
return collaborateurMetier;
}
public void setCollaborateurMetier(CollaborateurMetier collaborateurMetier) {
this.collaborateurMetier = collaborateurMetier;
}
public List<Collaborateur> getListeCollaborateurs() {
return listeCollaborateurs;
}
public void setListeCollaborateurs(List<Collaborateur> listeCollaborateurs) {
this.listeCollaborateurs = listeCollaborateurs;
}
public int getIdCollaborateur() {
return idCollaborateur;
}
public void setIdCollaborateur(int idCollaborateur) {
this.idCollaborateur = idCollaborateur;
}
public String getCin() {
return cin;
}
public void setCin(String cin) {
this.cin = cin;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public Date getDateNaissance() {
return dateNaissance;
}
public void setDateNaissance(Date dateNaissance) {
this.dateNaissance = dateNaissance;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
org.primefaces.spring.scope.ViewScope
package org.primefaces.spring.scope;
import java.util.Map;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
public class ViewScope implements Scope {
public Object get(String name, ObjectFactory objectFactory) {
Map<String,Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
if(viewMap.containsKey(name)) {
return viewMap.get(name);
} else {
Object object = objectFactory.getObject();
viewMap.put(name, object);
return object;
}
}
public Object remove(String name) {
return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
}
public String getConversationId() {
return null;
}
public void registerDestructionCallback(String name, Runnable callback) {
//Not supported
}
public Object resolveContextualObject(String key) {
return null;
}
}
CollaborateurDao
package dao;
import java.util.List;
import net.vo.Collaborateur;
public interface CollaborateurDao {
#SuppressWarnings("rawtypes")
public List getAllCollaborateurs();
}
CollaborateurHibernateDao.java
package dao;
import java.util.List;
import net.vo.Collaborateur;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
#Repository
public class CollaborateurHibernateDao implements CollaborateurDao{
private List<Collaborateur> listeCollaborateurs;
private Collaborateur collaborateur;
public void init()
{
System.out.println("Méthode d'initiation");
}
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public List getAllCollaborateurs() {
Session session=HibernateUtil.getSession();
try
{
session.beginTransaction();
Query q = session.createQuery("from Collaborateur");
listeCollaborateurs = q.list();
System.out.println(listeCollaborateurs.size());
return listeCollaborateurs;
}
catch(HibernateException e)
{
throw e;
}
finally
{
session.close();
}
}
}
CollaborateurMetier.java
package model;
import java.util.List;
import net.vo.Collaborateur;
public interface CollaborateurMetier {
public List<Collaborateur> getAllCollaborateurs();
}
CollaborateurMetierImpl.java
package model;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.CollaborateurDao;
import net.vo.Collaborateur;
#Service
public class CollaborateurMetierImpl implements CollaborateurMetier{
#Autowired
private CollaborateurDao collaborateurDao;
#SuppressWarnings("unchecked")
#Override
public List<Collaborateur> getAllCollaborateurs()
{
return collaborateurDao.getAllCollaborateurs();
}
public void setCollaborateurDao(CollaborateurDao collaborateurDao) {
this.collaborateurDao = collaborateurDao;
}
public CollaborateurDao getCollaborateurDao() {
return collaborateurDao;
}
}
Collaborateur.java
package net.vo;
// Generated 22 nov. 2014 20:53:37 by Hibernate Tools 4.3.1
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Collaborateur generated by hbm2java
*/
#Entity
#Table(name = "collaborateur", catalog = "formation")
public class Collaborateur implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int idCollaborateur;
private String cin;
private String nom;
private String prenom;
private Date dateNaissance;
private String telephone;
private String email;
public Collaborateur() {
}
public Collaborateur(int idCollaborateur, String cin) {
this.idCollaborateur = idCollaborateur;
this.cin = cin;
}
public Collaborateur(int idCollaborateur, String cin, String nom,
String prenom, Date dateNaissance, String telephone, String email) {
this.idCollaborateur = idCollaborateur;
this.cin = cin;
this.nom = nom;
this.prenom = prenom;
this.dateNaissance = dateNaissance;
this.telephone = telephone;
this.email = email;
}
#Id
#Column(name = "id_collaborateur", unique = true, nullable = false)
public int getIdCollaborateur() {
return this.idCollaborateur;
}
public void setIdCollaborateur(int idCollaborateur) {
this.idCollaborateur = idCollaborateur;
}
#Column(name = "cin", nullable = false, length = 254)
public String getCin() {
return this.cin;
}
public void setCin(String cin) {
this.cin = cin;
}
#Column(name = "nom", length = 254)
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
#Column(name = "prenom", length = 254)
public String getPrenom() {
return this.prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "dateNaissance", length = 19)
public Date getDateNaissance() {
return this.dateNaissance;
}
public void setDateNaissance(Date dateNaissance) {
this.dateNaissance = dateNaissance;
}
#Column(name = "telephone", length = 254)
public String getTelephone() {
return this.telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
#Column(name = "email", length = 254)
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
}
edit
This is what i get when i debug collaborateur.xhtml on the console. I'm using jboss 7
17:10:44,419 INFOS [org.jboss.modules] JBoss Modules version 1.1.1.GA
17:10:44,622 INFO [org.jboss.msc] JBoss MSC version 1.0.2.GA
17:10:44,684 INFO [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final "Brontes" starting
17:10:45,589 INFO [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-http)
17:10:45,589 INFO [org.xnio] XNIO Version 3.0.3.GA
17:10:45,604 INFO [org.xnio.nio] XNIO NIO Implementation Version 3.0.3.GA
17:10:45,604 INFO [org.jboss.remoting] JBoss Remoting version 3.2.3.GA
17:10:45,640 INFO [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
17:10:45,642 INFO [org.jboss.as.configadmin] (ServerService Thread Pool -- 26) JBAS016200: Activating ConfigAdmin Subsystem
17:10:45,704 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 38) JBAS011800: Activating Naming Subsystem
17:10:45,720 INFO [org.jboss.as.osgi] (ServerService Thread Pool -- 39) JBAS011940: Activating OSGi Subsystem
17:10:45,735 INFO [org.jboss.as.security] (ServerService Thread Pool -- 44) JBAS013101: Activating Security Subsystem
17:10:45,735 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 31) JBAS010280: Activating Infinispan subsystem.
17:10:45,735 INFO [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
17:10:45,735 INFO [org.jboss.as.connector] (MSC service thread 1-2) JBAS010408: Starting JCA Subsystem (JBoss IronJacamar 1.0.9.Final)
17:10:45,751 INFO [org.jboss.as.naming] (MSC service thread 1-1) JBAS011802: Starting Naming Service
17:10:45,751 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
17:10:45,782 INFO [org.jboss.as.security] (MSC service thread 1-6) JBAS013100: Current PicketBox version=4.0.7.Final
17:10:45,798 INFO [org.jboss.as.mail.extension] (MSC service thread 1-5) JBAS015400: Bound mail session [java:jboss/mail/Default]
17:10:46,078 INFO [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-5) JBoss Web Services - Stack CXF Server 4.0.2.GA
17:10:46,145 INFO [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-6) Démarrage de Coyote HTTP/1.1 sur http-localhost-127.0.0.1-8080
17:10:46,566 INFO [org.jboss.as.remoting] (MSC service thread 1-3) JBAS017100: Listening on localhost/127.0.0.1:9999
17:10:46,566 INFO [org.jboss.as.remoting] (MSC service thread 1-5) JBAS017100: Listening on localhost/127.0.0.1:4447
17:10:46,582 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-4) JBAS015012: Started FileSystemDeploymentService for directory C:\Program Files\jboss-as-7.1.1.Final\standalone\deployments
17:10:46,661 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found MyPro.war in deployment directory. To trigger deployment create a file called MyPro.war.dodeploy
17:10:46,661 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found Projet.war in deployment directory. To trigger deployment create a file called Projet.war.dodeploy
17:10:46,661 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found Projet_sqli.war in deployment directory. To trigger deployment create a file called Projet_sqli.war.dodeploy
17:10:46,739 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
17:10:46,754 INFO [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015876: Starting deployment of "MyPro.war"
17:10:46,754 INFO [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015876: Starting deployment of "Projet_sqli.war"
17:10:47,430 INFO [org.jboss.web] (MSC service thread 1-4) JBAS018210: Registering web context: /MyPro
17:10:48,853 WARN [org.jboss.as.server.deployment] (MSC service thread 1-5) JBAS015893: Encountered invalid class name 'com.sun.faces.vendor.Tomcat6InjectionProvider:org.apache.catalina.util.DefaultAnnotationProcessor' for service type 'com.sun.faces.spi.injectionprovider'
17:10:48,853 WARN [org.jboss.as.server.deployment] (MSC service thread 1-5) JBAS015893: Encountered invalid class name 'com.sun.faces.vendor.Jetty6InjectionProvider:org.mortbay.jetty.plus.annotation.InjectionCollection' for service type 'com.sun.faces.spi.injectionprovider'
17:10:49,260 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-3) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
17:10:49,260 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-3) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.fabric.jdbc.FabricMySQLDriver (version 5.1)
17:10:49,275 INFO [org.apache.catalina.core.StandardContext] (MSC service thread 1-1) The listener "com.sun.faces.config.ConfigureListener" is already configured for this context. The duplicate definition has been ignored.
17:10:49,306 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Projet_sqli]] (MSC service thread 1-1) Initializing Spring root WebApplicationContext
17:10:49,306 INFO [org.springframework.web.context.ContextLoader] (MSC service thread 1-1) Root WebApplicationContext: initialization started
17:10:49,322 INFO [org.springframework.web.context.support.XmlWebApplicationContext] (MSC service thread 1-1) Refreshing org.springframework.web.context.support.XmlWebApplicationContext#ccce74: display name [Root WebApplicationContext]; startup date [Wed Dec 03 17:10:49 WET 2014]; root of context hierarchy
17:10:49,353 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-1) Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
17:10:49,696 INFO [org.springframework.web.context.support.XmlWebApplicationContext] (MSC service thread 1-1) Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext#ccce74]: org.springframework.beans.factory.support.DefaultListableBeanFactory#7095b
17:10:49,759 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-1) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#7095b: defining beans [org.springframework.beans.factory.config.CustomScopeConfigurer#0,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
17:10:49,759 INFO [org.springframework.web.context.ContextLoader] (MSC service thread 1-1) Root WebApplicationContext: initialization completed in 453 ms
17:10:49,774 INFOS [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-1) Initialisation de Mojarra 2.1.7-jbossorg-1 (20120227-1401) pour le contexte «/Projet_sqli»
17:10:50,696 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-1) Hibernate Validator 4.2.0.Final
17:10:51,477 INFOS [org.primefaces.webapp.PostConstructApplicationEventListener] (MSC service thread 1-1) Running on PrimeFaces 5.1
17:10:51,492 INFOS [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-1) Monitoring jndi:/default-host/Projet_sqli/WEB-INF/faces-config.xml for modifications
17:10:51,508 INFO [org.jboss.web] (MSC service thread 1-1) JBAS018210: Registering web context: /Projet_sqli
17:10:51,508 INFO [org.jboss.as] (MSC service thread 1-4) JBAS015951: Admin console listening on http://127.0.0.1:9990
17:10:51,508 INFO [org.jboss.as] (MSC service thread 1-4) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 7401ms - Started 319 of 398 services (78 services are passive or on-demand)
17:10:51,539 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "Projet_sqli.war"
17:10:51,539 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "MyPro.war"

i found a solution for this problem. I had missing dependencies and i had to delete some dependencies because of problems in versions
this is my new pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ma.ensa</groupId>
<artifactId>Projet_sqli</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Projet_sqli Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.7.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.8-02</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.8-02</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
</repositories>
<build>
<finalName>Projet_sqli</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

Related

why I get 404 status Spring MVC

I don't know why browser doesn't find view. I use maven and tomcat 9.0.69
In URL I write 'http://localhost:8080' and it returns error with 404 status
Code below
Controller
package ru.alishev.controllers;
import ru.alishev.dao.PersonDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class PeopleController {
private final PersonDAO personDAO;
//можно не писать #Autowired, spring внедрит эту зависимость
#Autowired
public PeopleController(PersonDAO personDAO) {
this.personDAO = personDAO;
}
#GetMapping()
public String index(Model model){
model.addAttribute("people", personDAO.index());
return "index";
}
}
Configuration
package ru.alishev.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MySpringMVCDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return null;
}
protected Class<?>[] getServletConfigClasses() {
return new Class[] {SpringConfig.class};
}
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
package ru.alishev.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
#Configuration
#ComponentScan("ru.alishev")
#EnableWebMvc
public class SpringConfig implements WebMvcConfigurer {
private final ApplicationContext applicationContext;
#Autowired
public SpringConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
#Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
}
HTML file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Все люди</title>
</head>
<body>
Hello world!
</body>
</html>
Maven
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Test</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>6.0.0</spring.version>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.15.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>Test</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
OUTPUT
23-Nov-2022 06:31:47.386 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [18] milliseconds
Connected to server
[2022-11-23 06:31:47,559] Artifact Test:war exploded: Artifact is being deployed, please wait...
23-Nov-2022 06:31:47.641 WARNING [RMI TCP Connection(2)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [5.0]. Default version will be used.
23-Nov-2022 06:31:48.090 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2022-11-23 06:31:48,104] Artifact Test:war exploded: Artifact is deployed successfully
[2022-11-23 06:31:48,104] Artifact Test:war exploded: Deploy took 545 milliseconds
23-Nov-2022 06:31:57.395 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Установка веб приложения в папку [/Users/valeriali/Downloads/apache-tomcat-9.0.69/webapps/manager]
23-Nov-2022 06:31:57.449 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/Users/valeriali/Downloads/apache-tomcat-9.0.69/webapps/manager] has finished in [55] ms
In this picture the structure of project
[1]: https://i.stack.imgur.com/N4lvo.png

JBAS015852: Could not index class module-info.class - Spring boot + Jboss 7.1.1

I am trying to deploy a simple Spring boot application on a Jboss 7.1.1.
I have made the corresponding settings, but the error continuously appears:
"JBAS015852: Could not index class module-info.class"
I made the following settings:
#SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#RestController
class Hellocontroller {
#RequestMapping("/hello")
#GetMapping
String hello() {
return "Hola";
}
}
and in the pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
but I have the following result
16:12:16,317 WARN [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015852: Could not index class org/hibernate/validator/spi/scripting/AbstractCachingScriptEvaluatorFactory.class at /C:/Users/User/Downloads/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final/standalone/deployments/demo.war/WEB-INF/lib/hibernate-validator-6.0.19.Final.jar: java.lang.IllegalStateException: Unknown tag! pos=4 poolCount = 71
at org.jboss.jandex.Indexer.processConstantPool(Indexer.java:606) [jandex-1.0.3.Final.jar:1.0.3.Final]
at org.jboss.jandex.Indexer.index(Indexer.java:640) [jandex-1.0.3.Final.jar:1.0.3.Final]
at org.jboss.as.server.deployment.annotation.ResourceRootIndexer.indexResourceRoot(ResourceRootIndexer.java:77) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.server.deployment.annotation.AnnotationIndexProcessor.deploy(AnnotationIndexProcessor.java:51) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_80]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_80]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_80]
I need your support, thanks.
The problem was due to dependency conflict, which I could exclude using the following jboss-deployment-structure.xml
<?xml version='1.0' encoding='UTF-8'?>
<jboss-deployment-structure
xmlns="urn:jboss:deployment-structure:1.1">
<deployment>
<exclusions>
<module name="com.fasterxml.jackson.core.jackson-annotations" />
<module name="com.fasterxml.jackson.core.jackson-core" />
<module name="com.fasterxml.jackson.core.jackson-databind" />
<module
name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider" />
<module name="org.jboss.resteasy.resteasy-jackson2-provider" />
<module name="org.slf4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>

Using JSF and Spring 3, the BO is always null (when using an interface on your BO) [duplicate]

This question already has answers here:
Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
(4 answers)
Closed 6 years ago.
What I have is a simple hello world application utilizing JSF and Spring with Maven. Whenever I call on my BO (business object) from within my Managed Bean the BO is always null. I am not sure what I am missing or not understanding.
HelloWorldMB.java
package com.project.web;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import tech.calvanodesign.business.HelloWorldBo;
import java.io.Serializable;
#ManagedBean
#SessionScoped
public class HelloWorldMB implements Serializable {
public HelloWorldBo helloWorldBo;
private static final long serialVersionUID = 1L;
private String name;
public void init () {
System.out.println("HelloWorldMB.init()");
if (helloWorldBo != null)
return;
System.out.println("helloWorldBo is null");
}
public String springTest() {
// Call the business object to register the user
helloWorldBo.springTest(name);
return "";
}
// Set the registrationBo attribute used by Spring
public void setHelloWorldBo(HelloWorldBo helloWorldBo) {
this.helloWorldBo = helloWorldBo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
welcome.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>JSF 2.0 Hello World</title>
<h:outputStylesheet library="css" name="style.css" />
</h:head>
<h:body>
<h:form>
<p:growl id="growl" showDetail="true" sticky="true" />
<p:inputText id="intxtSpringTest" value="#{helloWorldMB.name}"/>
<p:commandButton id="cmdbtnSpringTest" value="Test Spring 3 with JSF" action="#{helloWorldMB.springTest}" ajax="false"/>
</p:panel>
</h:form>
</h:body>
HelloWorldBo.java
package com.project.business;
public interface HelloWorldBo {
/**
* springTest method
* #param name
*/
public void springTest(String name);
}
HelloWorldBoImpl
package com.project.business;
public class HelloWorldBoImpl implements HelloWorldBo {
/**
* Tests the spring and jsf implementation
*/
#Override
public void springTest(String name) {
System.out.println("HelloWorldBoImpl:: springTest : " + name);
}
}
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tech.calvanodesign</groupId>
<artifactId>calvanodesignsource</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>calvanodesignsource Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.0</version>
</dependency>
<!-- spring-context which provides core functionality -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>calvanodesignsource</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
https://github.com/Epooch/CalvanoDesignSource
The source for those who want to see the whole application as it is on my machine.
The code blocks will remain as it was for those who run into the issues. I will post below it the changes that I had made to make it work.
Working Solution
HelloWorldMB
package com.project.web;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import com.project.business.HelloWorldBo;
import java.io.Serializable;
#ManagedBean
#SessionScoped
public class HelloWorldMB implements Serializable {
#ManagedProperty(value = "#{helloWorldBo}")
private HelloWorldBo helloWorldBo;
private static final long serialVersionUID = 1L;
private String name;
public void init () {
System.out.println("HelloWorldMB.init()");
if (helloWorldBo != null)
return;
System.out.println("helloWorldBo is null");
}
public void springTest(ActionEvent e) {
// Call the business object to register the user
helloWorldBo.springTest(name);
}
// Set the registrationBo attribute used by Spring
public void setHelloWorldBo(HelloWorldBo helloWorldBo) {
this.helloWorldBo = helloWorldBo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HelloWorldBo
package com.project.business;
public interface HelloWorldBo {
/**
* springTest method
* #param name
*/
public void springTest(String name);
}
HelloWorldBoImpl
package com.project.business;
import javax.inject.Named;
#Named("helloWorldBo")
public class HelloWorldBoImpl implements HelloWorldBo {
/**
* Tests the spring and jsf implementation
*/
#Override
public void springTest(String name) {
System.out.println("HelloWorldBoImpl:: springTest : " + name);
}
}
Added the following dependency to the pom.xml
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
If you expect Spring to inject the business object, you will have to provide JSF with some way to resolve the bean references. Your managed bean must initialize the business object somewhere in a method that will be invoked during the JSF lifecycle.
For example, here are the relevant sections from a simple example.
First, you need the Spring setup in the web application descriptors:
/WEB-INF/web.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/app-service-config.xml
</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>
/WEB-INF/app-service-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring 3.1 annotation support -->
<context:component-scan base-package="com.rtt.simple.service" />
Then you need to set up JSF with a resolver that lets it inject Spring beans in its managed beans.
faces-config.xml
<application>
<!-- Spring Framework support -->
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
Then, you can inject a Spring bean in a managed bean:
DummyBackingBean.java
#ManagedBean
#ViewScoped
public class DummyBackingBean implements Serializable {
#ManagedProperty(value = "#{dummyService}")
private DummyService dummyService;
private List<DummyDataItem> dataItems;
#PostConstruct
public void postConstruct() {
LOG.trace("postConstruct()");
dataItems = dummyService.listAll();
}
public DummyService getDummyService() {
return dummyService;
}
public void setDummyService(DummyService dummyService) {
this.dummyService = dummyService;
}
DummyService.java
package com.rtt.simple.service;
import javax.inject.Named;
import com.rtt.simple.domain.DummyDataItem;
#Named("dummyService")
public class DummyService {
private static List<DummyDataItem> dataItems;
public List<DummyDataItem> listAll() {
return dataItems;
}
static {
dataItems = new ArrayList<DummyDataItem>();
// Initialize the dataItems list with static data
Note that I've used the #Named annotation from javax.inject to declare the bean in the Spring configuration, but this technique will work with any Spring injection annotation.

Getting Null Pointer Exception implementing JAX_RS

I am new to JAX-RS. I am doing CRUD operation using JAX_RS. CRUD operation is working fine for JSF but when I call fetchAll() method present in Service layer using JAX-RS I am getting a NullPointerException.
Below is my JAX_RS code
package com.resource;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.neorays.resource.entity.Person;
import com.neorays.resource.service.PersonService;
#Path("/persons")
public class MyResource {
PersonService ser = new PersonService();
#GET
#Path("/pers")
#Produces(MediaType.APPLICATION_JSON)
public List<Person> getAllPerson() {
List<Person> list = null;
try {
list = ser.fetchAll();
for (Person person : list) {
person.getId();
System.out.println("person id is" + person.getId());
System.out.println("person name is" + person.getName());
System.out.println("person country is" + person.getCountry());
}
} catch (NullPointerException ne) {
System.out.println("null pt is occuring" + ne);
}
return list;
}
}
This is my Service Layer
package com.resource.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.neorays.resource.bean.PersonBean;
import com.neorays.resource.dao.PersonDao;
import com.neorays.resource.entity.Person;
#Component
#Transactional
public class PersonService
{
#Autowired
private PersonDao dao;
#Transactional
public List<Person> fetchAll()
{
return dao.fetchAll();
}
}
This is my DAO layer
package com.resource.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.neorays.resource.bean.PersonBean;
import com.neorays.resource.entity.Person;
#Repository
public class PersonDao
{
#Autowired
private SessionFactory sessionFactory;
public List<Person> fetchAll()
{
Session ses = sessionFactory.getCurrentSession();
String query="from Person";
Query query2 =ses.createQuery(query);
List<Person> list = query2.list();
return list;
}
}
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</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>/faces/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.neorays.resource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
This is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.resource</groupId>
<artifactId>jsf_jaxrs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<finalName>jsf_jaxrs</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Finally, this is my Bean
package com.neorays.resource.entity;
import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
#Entity
#NamedQuery(name="Person.findAll", query="SELECT p FROM Person p")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String country;
private String name;
public Person() {
}
public Person(int id, String country, String name) {
super();
this.id = id;
this.country = country;
this.name = name;
}
#Id
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Your dependency injection is not constructed correctly. All of your beans must be created by Spring. If your applicationContext.xml file scan necessary packages (for services and daos at least), and if your hibernate session factory created correctly and since you already tell jersey to scan package for rest services this should work.
#Component
#Path("/persons")
public class MyResource {
#Autowired
PersonService ser;
...
}
Update
I just realise package name in web.xml and your rest service are different.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
....
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.resource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
....
</web-app>
If jersey-spring3 is missing in your class path you can add it using maven.
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey.version}</version>
</dependency>
You can find additional info on jersey documentation.

Spring DI - Autowired property is null in a REST service

I'm getting started with Spring DI, but I'm struggling with dependency injection and the worse part is that I'm not even sure why as it seems ok to me. Hopefully you guys can help me out!
The problem is that a property annotated as #Autowired is always null
I've got a few projects with Maven structure:
com.diegotutor.lessondeliver
com.diegotutor.utility
I'm running the examples over Tomcat 7
I'm using the following dependencies in my pom.xml:
spring-context 3.2.4
spring-web 3.2.4
jersey-server 1.17.1
jersey-core 1.17.1
jersey-servlet 1.17.1
The simple idea is to have a RESTful service that through Dependency Injection is able to print out the value of a property located in a config file located at: D:\configuracion.conf.
At com.diegotutor.utility I have the following interface:
package com.diegotutor.utility;
public interface ConfigService {
public String getProperty(final String propertyName);
}
Implemented by:
package com.diegotutor.utility.impl;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;
import com.diegotutor.utility.ConfigService;
public class PropertyFileConfigService implements ConfigService{
Properties prop;
public PropertyFileConfigService (final InputStream input) throws IOException {
if(input == null) {
throw new IllegalArgumentException("Input stream can't be null");
}
prop = new Properties();
prop.load(input);
}
public PropertyFileConfigService (final String fileName) throws IOException {
final FileInputStream input = new FileInputStream(fileName);
prop = new Properties();
prop.load(input);
}
public PropertyFileConfigService(final Reader input) throws IOException {
prop = new Properties();
prop.load(input);
}
public String getProperty(final String propertyName) {
return prop.getProperty(propertyName);
}
}
And at com.diegotutor.lessondeliver I have the RESTful service where I would like to use an injected instance of the ConfigService:
package com.diegotutor.lessondeliver;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.diegotutor.utility.ConfigService;
#Path("/")
#Component
public class HelloWorld {
private static final Log log = LogFactory.getLog(HelloWorld.class);
#Autowired
private ConfigService configService;
#Path("/helloworld")
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getHello() {
String host = configService.getProperty("host");
return "Hello World! HOST" + host;
// configService IS NULL!!
//SO IT THROWS A NULLPOINTER EXCEPTION WHEN INVOKING getProperty ON IT
}
}
Finally at /com.diegotutor.lessondeliver/src/main/webapp/WEB-INF/service-beans.xml I have the following XML application context file, where I use the implementation of ConfigService (PropertyFileConfigService) injecting on it the path for the configuration file to read:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="configService" class="com.diegotutor.utility.impl.PropertyFileConfigService">
<constructor-arg type="java.lang.String"
value="D:\configuracion.conf" />
</bean>
<context:component-scan base-package="com.diegotutor" />
</beans>
Obviously I have specified in the web.xml of this com.diegotutor.lessondeliver web app that I want service-beans.xml as ConfigLocation and a listener ContextLoaderListener, and the RESTful service relies on ServletContainer
If I'm specifying context:component-scan to look for Components in com.diegotutor as suggested here and I'm forcing object creation through Spring by not using any new Statement as suggested here, Why am I getting the annotated configService as null? Why Spring is unable to inject an instance of com.diegotutor.utility.impl.PropertyFileConfigService?
Any help will be much appreciated!
Thank you
EDITED:
As requested, my web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>com.diegotutor.lessondeliver</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/service-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
You were right!
It seems that the problem is that Jersey is totally unaware of Spring and instantiates its own object. In order to make Jersey aware of Spring object creations (through dependency injection) I had to integrate Spring + Jersey.
To integrate:
Add maven dependencies
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.17.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>
Use SpringServlet for jersey-servlet in web.xml
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Now the #Autowired works properly and the object is not null anymore.
I'm a little bit confused about the exclusions I have to use in maven when using jersey-spring dependency, but that's another issue :)
Thank you!
Integration Spring with Jersey 2 (org.glassfish.*):
Maven
Some dependencies may be unnecessary, please check & clear it after things got working.
<properties>
<jersey.version>2.5</jersey.version>
</properties>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- Jersey + Spring -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
web.xml
<servlet>
<servlet-name>my-rest-service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>my.package.with.rest.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my-rest-service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
applicationContext.xml
During the Spring upgrading I had to move it from /main/webapp/WEB-INF/ to /main/resources/ (details).
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="my.package.with.rest.services" />
</beans>
Example REST service
public interface MyService
{
String work(String s);
}
...
#Service
public class MyServiceImpl implements MyService
{
#Override
public String work(String s)
{
return "Hello, " + s;
}
}
...
#Path("demo/")
#Component
public class DemoRestService
{
#Autowired
private MyService service;
#GET
#Path("test")
public Response test(#FormParam("param") String par)
{
try
{
String entity = service.work(par);
return Response.ok(entity).build();
}
catch (Exception e)
{
e.printStackTrace();
return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Epic REST Failure").build();
}
}
}
or you can simply extend SpringBeanAutoWiringSupport class. Like this: public class DemoRestService extends SpringBeanAutoWiringSupport. By extending this support class, properties of your service class can be auto-wired.
Another possible option is to manually invoke autowiring in your jersey resource:
#Context
private ServletContext servletContext;
#PostConstruct
public void init() {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, servletContext);
}
Hmm, you get a "manual autowiring"...

Resources