我想编写通用服务和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. 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,这是整个stacktrace。

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方法(注释行)时,它可以工作。能给我个建议吗?

最佳答案

我相信您的问题如下:

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


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

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

10-05 18:52