【问题标题】:DAO nullpointer exception in spring serviceSpring服务中的DAO空指针异常
【发布时间】:2014-03-09 22:25:47
【问题描述】:

我在 spring 服务中遇到了一个 DAO 的问题,DAO 没有正确实例化。 这是我的 DAO、DAOImp、Service 和 ServiceImp 以及 beans.xml 文件

package com.dao;

import java.util.List;
import com.dto.ProductDTO;

public interface ProductDAO {
    public List<ProductDTO> getAllProducts();
}

package com.dao.implementations;

import java.util.ArrayList;
import java.util.List;

import com.dao.ProductDAO;
import com.dto.ProductDTO;

public class ProductDAOImp implements ProductDAO{
    @Override
    public List<ProductDTO> getAllProducts() {
        List<ProductDTO> liste = new ArrayList<ProductDTO>();

        liste.add(new ProductDTO(1,"pc", 100));
        liste.add(new ProductDTO(2,"disk", 11));
        return liste;
    }
}

package com.webservices;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


import com.dto.ProductDTO;

@Path("products")
public interface ProductWebService {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("all")
    public List<ProductDTO> getAllProducts();
}

网络服务实现:

package com.webservices.implementations;

import java.util.List;

import com.dao.implementations.ProductDAOImp;
import com.dto.ProductDTO;
import com.webservices.ProductWebService;


public class ProductWebServiceImp implements ProductWebService {

    private ProductDAOImp productDAO;


    @Override
    public List<ProductDTO> getAllProducts() {  
        return productDAO.getAllProducts();
    }

    public ProductDAOImp getProductDAO() {
        return productDAO;
    }

    public void setProductDAO(ProductDAOImp productDAO) {
        this.productDAO = productDAO;
    }
}

beans.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"    
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"   
    xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation=" 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://cxf.apache.org/jaxws
     http://cxf.apache.org/schemas/jaxws.xsd
     http://cxf.apache.org/jaxrs
     http://cxf.apache.org/schemas/jaxrs.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="productWebService"
    serviceClass="com.webservices.implementations.ProductWebServiceImp"
    name="productWebService" address="/productServices" />




<bean id="productService"     
          class="com.webservices.implementations.ProductWebServiceImp">
    <property name="productDAO" ref="productDAO"></property>
</bean>

<bean id="productDAO" class="com.dao.implementations.ProductDAOImp"/>
    </beans>

问题是在服务中调用getAllProducts()方法时会抛出nullpointer异常(属性dao为null)

我的代码有什么问题吗? 感谢您的帮助

【问题讨论】:

  • 您似乎缺少 Spring 和 JAX-RS 集成。
  • 我不这么认为,因为如果我在 spring 服务的 getAllProducts 方法中实例化 DAO,一切正常
  • 这似乎与 Spring 或 JAX-RS 无关。该字段为null,如果实例化它,则不再是null。那不是重点。在我看来,您在 beans.xml 中定义的 productService bean 不是抛出 NPE 的那个。 JAX-RS 可能正在创建自己的 ProductWebServiceImp 实例。
  • 一开始我已经创建了DAO Spring服务和Web服务,但是我注意到Web服务只是调用服务女巫轮流调用dao,所以我决定去掉服务,只保留web服务与道

标签: java spring service dao


【解决方案1】:

您好,这是一个对我有用的解决方案:

<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation=" 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://cxf.apache.org/jaxws
     http://cxf.apache.org/schemas/jaxws.xsd
     http://cxf.apache.org/jaxrs
     http://cxf.apache.org/schemas/jaxrs.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<context:component-scan base-package="com.webservices.implementations" />
<context:annotation-config />

<jaxrs:server id="productWebService" name="productWebService"
    address="/productServices">
    <jaxrs:serviceBeans>
        <ref bean="productService"/>
    </jaxrs:serviceBeans>
</jaxrs:server>

<bean id="productService" class="com.webservices.implementations.ProductWebServiceImp">
  <property name="productDAO" ref="productDAO"/>
</bean>

<bean id="productDAO" class="com.dao.implementations.ProductDAOImp" />

感谢所有参与者

最好的问候

【讨论】:

    【解决方案2】:

    您可以尝试在您的 Dao 上的 ProductWebServiceImp 中添加 @Autowired 注解。

    【讨论】:

    • 同样的错误,建议是基于注解的,和我在 beans.xml 文件中所做的一样
    • 您尝试添加注释:@Autowired public void setProductDAO(ProductDAOImp productDAO) { this.productDAO = productDAO; },在您的 xml 中,您声明了 bean,并带有注释,您说 Spring 使用它。注释必须在公共设置器上。
    猜你喜欢
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多