-
Sep 12th, 2011, 09:07 PM
#1
Spring3 MVC + JPA2+Hibernate with tomcat
Here is my case, got stucked from last 2 days.
1) I have an application which packaged into a jar file, this typically had only
a) persistence.xml and spring configuration file (persistenceContext.xml)
b) entity classes
c) dao classes with @Repository annotation and also defined in persistenceContext.xml
d) service classes which are defined in persistenceContext.xml file.
service classes uses daos, which are injected through @Resource annotation.
It is packed into a jar file shared.jar .
When ran the service unit tests in shared.jar , which are working fine.
2) There is a spring mvc web application which uses shared.jar file.
included the shared.jar into lib and from controller trying to access the services by autowiring mechanism.
Just added <import resource="classpath*:persistenceContext.xml" /> to servlet spring configuration file.
If i remove the <import> statement getting no such services are defined exception. after adding the <import> getting null pointer on services,
the services are not being injected into controllers.
when the moment of debugging found services are null.
Please help me out.
-
Sep 13th, 2011, 05:12 AM
#2
Post your configuration and the full stack trace you get! When doing so use [ code][/code ] tags..
-
Sep 15th, 2011, 04:15 AM
#3
Thanks Martin,
Here is my code.
<code>
1) Entity Class
package com.product.web.db.domain;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User implements Serializable{
@Id
private String userId;
@Column(name = "user_type")
private String userType;
@Column(name = "user_name")
private String userName;
@Column(name = "created_date")
private Date createdDate;
........... Setters & Getters -----------
}
2) Dao Classes
package com.product.web.db.dao;
import com.product.web.db.domain.User;
public interface IUserDao {
public void save(User user);
public void delete(User user);
public User getUser(String userId);
}
package com.product.web.db.dao.impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.fairchildsemi.fscwebdb.vo.Asset;
import com.product.web.db.dao.IUserDao;
import com.product.web.db.domain.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transac tional;
@Transactional
@Repository(value = "userDao")
public class UserDaoImpl implements IUserDao {
@PersistenceContext(unitName = "webUnit")
private EntityManager entityManager;
---setter & getter for entityManager-----
@Override
public void save(User user) {
entityManager.persist(user);
}
@Override
public void delete(User user) {
entityManager.remove(user);
}
@Override
public User getUser(String userId) {
return entityManager.find(User.class, userId);
}
}
3) Service Classes
package com.product.web.db.service;
import com.product.web.db.domain.User;
public interface IUserService {
public void saveUser(User user);
public void deleteUser(User user);
public User findUserById(String userId);
}
package com.product.web.db.service.impl;
import javax.annotation.Resource;
import com.product.web.db.dao.IUserDao;
import com.product.web.db.domain.User;
import com.product.web.db.service.IUserService;
public class UserServiceImpl implements IUserService {
@Resource(name = "userDao")
private IUserDao userDao; // Here i am getting the null pointer. (NPE)
@Override
public void saveUser(User user) {
userDao.save(user);
}
@Override
public void deleteUser(User user) {
userDao.delete(user);
}
@Override
public User findUserById(String userId) {
return userDao.getUser(userId);
}
}
4) configuration files
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="webUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence </provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.max_fetch_depth" value="5" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@WEBDEV-01:1521:webdev01" />
<property name="hibernate.connection.username" value="webDB" />
<property name="hibernate.connection.password" value="webDBdev" />
</properties>
</persistence-unit>
</persistence>
persistenceContext.xml
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schem...ing-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.product.web.db" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
<property name="persistenceUnitName" value="webUnit" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
</bean>
<bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.context.annotation.Comm onAnnotationBeanPostProcessor">
<property name="fallbackToDefaultTypeMatch" value="false" />
</bean>
<bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.Persiste nceExceptionTranslationPostProcessor" />
<bean id="assetDAO" class="com.product.web.db.dao.impl.UserDaoImpl" />
</beans>
</code>
All these classes, configuration files packaged in to a jar file shared.jar.
folder structure.JPG
There is another Spring MVC web application, where i am using this standard.jar file. Trying to autowire Services in controller but which is successful.
but when the dao in service is not injected, which is null. Spring MVC Web application code
<code>
1) web.xml
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springContext/appServlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
2) appServlet-context.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd>
<import resource="classpath*:META-INF/persistenceContext.xml"/>
<context:component-scan base-package="com.product.externalweb.site.controller"/>
<bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter" />
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2 .TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs/templates.xml</value>
</list>
</property>
</bean>
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBas edViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesV iew
</value>
</property>
</bean>
<bean name="userService" class="com.product.web.db.service.impl.UserService Impl"/>
</beans>
3) Controller
@Controller
public class UserController extends MultiActionController {
private UserService userService = null;
public UserController() {
}
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
protected UserService getUserService() {
return userService;
}
public void setUserService(UserService service) {
this.userService = userService;
}
@RequestMapping("/userDetails")
public String processActive(Model model,String userId) {
try {
User user = userService.findUserById(userId);
model.addAttribute("userName", user.getUserName());
model.addAttribute("userType", user.getUserType());
model.addAttribute("createdDate", user.getCreatedDate);
} catch (Exception daoe) {
daoe.printStackTrace();
}
return "userDetails";
}
}
</code>
Where am wrong.
Please help me .
Thanks,
Uday
Last edited by udaykiranmca; Sep 15th, 2011 at 04:21 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules