vendredi 27 février 2015

Jax-ws spring jpa and hibernate in mysql PersistenceContext EntityManager injection NullPointerException

i'm using jax-ws with spring jpa and hibenate in mysql. Here i was facing some issue in autowired and entitymanager return null point exception. before i was used restful application working fine. After i convert restful webservice to jax-ws using soap web service. now i changed xml configuration and added depending jar file not working in autowired and jpa entitymanager. any one please guide me.


RestFulService.java



import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.springframework.beans.factory.annotation.Autowired;
import com.rs.transaction.TransactionBo;
import com.rs.transaction.impl.TransactionBoImpl;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class RestFulService {

@Autowired
TransactionBo transactionBo;

@WebMethod(operationName="getHelloWorld")
public String getHelloWorld(String name) {
return "Hello World JAX-WS " + name;
}

@WebMethod(operationName="getAllTransaction")
public String getAllTransaction() {
TransactionBo transactionBo = new TransactionBoImpl();
List<Employee> result = transactionBo.getAllTransaction();
StringBuffer buffer = new StringBuffer();
for (Employee emp : result) {
buffer.append(emp.getId()).append(":").append(emp.getFistName())
.append("\n");
}
System.out.println(""+buffer.toString());

return "success";
}
}


TransactionBo.java



public interface TransactionBo {

public List<Employee> getAllTransaction();


}

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import javax.xml.bind.annotation.XmlElement;
import org.springframework.stereotype.Service;
import com.rs.transaction.TransactionBo;

@Service
public class TransactionBoImpl implements TransactionBo{

private EntityManager entityManager;


public EntityManager getEntityManager() {
return entityManager;
}
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Transactional
@XmlElement(required=true, nillable=true)
public List<Employee> getAllTransaction(){
Query query = entityManager.createNamedQuery("Employee.findAll");
List<Employee> employee;
employee = query.getResultList();
return employee;

}

}


Persistence.xml



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://ift.tt/UICAJV" xmlns:xsi="http://ift.tt/ra1lAU" version="2.0" xsi:schemaLocation="http://ift.tt/UICAJV http://ift.tt/O9YdEP">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.rs.entity.Employee</class>

<!-- <properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
</properties> -->
</persistence-unit>
</persistence>


applicationContext.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
xmlns:tx="http://ift.tt/OGfeU2" xmlns:p="http://ift.tt/1jdM0fE"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/QEDs1e
http://ift.tt/GArMu7
http://ift.tt/QEDs1k
http://ift.tt/OGfeU2
http://ift.tt/18tm2Tg">

<!-- Classpath scanning of @Component, @Service, etc annotated class -->
<context:component-scan base-package="com.rs.entity,com.rs.rest,com.rs.transaction,com.rs.transaction.impl"/>
<context:annotation-config />
<tx:annotation-driven />

<!-- MySQL Datasource with Commons DBCP connection pooling -->
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="MYSQL" p:showSql="true" />


</beans>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://ift.tt/ra1lAU" xmlns="http://ift.tt/nSRXKP" xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/1eWqHMP" id="WebApp_ID" version="3.0">
<display-name>Restful Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

</web-app>


sun-jaxws.xml



<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://ift.tt/1qvaLJl"
version="2.0">
<endpoint
name="HelloWorldWs"
implementation="com.rs.rest.RestFulService"
url-pattern="/hello"/>
</endpoints>


pom.xml



<project xmlns="http://ift.tt/IH78KX" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jax-ws.spring</groupId>
<artifactId>JaxRsSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>JaxRsSpring</name>
<description>Jersey Restful web service application</description>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://ift.tt/1eIxoyJ;
</repository>
</repositories>

<dependencies>

<!-- Jersey -->
<!-- JAX-WS -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.3</version>
</dependency>

<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

<!-- Jersey + Spring -->
<dependency>
<groupId>org.jvnet.jax-ws-commons.spring</groupId>
<artifactId>jaxws-spring</artifactId>
<version>1.8</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>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.jvnet.staxex</groupId>
<artifactId>stax-ex</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.3.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.3.Final</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Aucun commentaire:

Enregistrer un commentaire