协助通用服务/ dao层

时间:2013-08-22 10:40:58

标签: java spring generics service dao

我想编写通用服务和DAO层。

这是使用过的实体的UML。 enter image description here

1。 AbstractDao的

public interface AbstractDAO<T> {

    public List<T> getAll();

}

2。 AbstractDAOImpl

@Repository
public abstract class AbstractDAOImpl<T> implements AbstractDAO<T> {

    private final Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<T> getAll() {
        String query = "from " + clazz.getSimpleName().toString();
        return sessionFactory.getCurrentSession().createQuery(query)
                .list();
    }

}

3。 PaymentTypeDAO

public interface PaymentTypeDAO extends AbstractDAO<PaymentType> {

}

4。 PaymentTypeDAOImpl

@Repository
public class PaymentTypeDAOImpl extends AbstractDAOImpl<PaymentType> implements PaymentTypeDAO {

    @Autowired
    private SessionFactory sessionFactory;

}

5。 AbstractService

@Transactional
public interface AbstractService<T> {

    @Transactional
    public List<T> getAll();

}

6。 AbstractServiceImpl

@Service
@Transactional
public class AbstractServiceImpl<T> implements AbstractService<T> {


    @Autowired
    private AbstractDAO<T> dao;

    @Transactional
    public List<T> getAll() {
        return dao.getAll();
    }

    protected AbstractDAO<T> getDao() {
        return dao;
    }

    protected void setDao(AbstractDAO<T> dao) {
        this.dao = dao;
    }

}

7。 PaymentTypeService

@Transactional
public interface PaymentTypeService extends AbstractService<PaymentType> {

}

8。 PaymentTypeServiceImpl

@Service
@Transactional
public class PaymentTypeServiceImpl extends AbstractServiceImpl<PaymentType> implements PaymentTypeService {

    @Autowired
    private PaymentTypeDAO paymentTypeDAO;

    public PaymentTypeServiceImpl() {
        super.setDao(paymentTypeDAO);
    }

//  @Override
//  public List<PaymentType> getAll() {
//      return paymentTypeDAO.getAll();
//  }
//  
}

我在控制器中自动装配PaymentTypeService,然后调用getAll()方法。但它仍然抛出NullPointerException,这里是整个堆栈跟踪。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

root cause

java.lang.NullPointerException
    com.app.cloud.service.AbstractServiceImpl.getAll(AbstractServiceImpl.java:29)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    com.sun.proxy.$Proxy207.getAll(Unknown Source)
    com.app.cloud.controller.orders.OrderController.edit(OrderController.java:150)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

当我在getAll中覆盖PaymentTypeServiceImpl方法(注释行)时,它可以正常工作。你能给我一个建议吗?

2 个答案:

答案 0 :(得分:1)

我相信您的问题如下:

public PaymentTypeServiceImpl() {
    super.setDao(paymentTypeDAO);
}

此时在代码中(在构造函数中)paymentTypeDAOnull,因为尚未调用setter。

您可以考虑在Spring上下文中使用property来传递DAO

答案 1 :(得分:0)

添加@Qualifier(“paymentTypeDAO”)并删除super.setDao(paymentTypeDAO);来自构造函数。

 @Service
 @Transactional
 public class PaymentTypeServiceImpl extends AbstractServiceImpl<PaymentType> implements PaymentTypeService {

        @Autowired
        @Qualifier("paymentTypeDAO")
        private PaymentTypeDAO paymentTypeDAO;

        public PaymentTypeServiceImpl() {

        }

}

请确保您的上下文文件中存在以下annotation-config,component-scan。

<?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-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="your package name here" />

 </beans>