2

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

这是使用实体的 UML。在此处输入图像描述

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.抽象服务

@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.支付类型服务

@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,它可以工作。你能给我一个建议吗?

4

2 回答 2

1

我相信您的问题如下:

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

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

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

于 2013-08-22T10:59:49.053 回答
0

添加 @Qualifier("paymentTypeDAO") 并删除 super.setDao(paymentTypeDAO); 从构造函数。

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

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

        public PaymentTypeServiceImpl() {

        }

}

请确保您的上下文文件中存在以下注释配置、组件扫描。

<?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>
于 2013-08-22T11:51:21.597 回答