문제

I am facing a problem with a DAO in a spring service, the DAO is not instantiated properly. here is my DAO, DAOImp, Service and ServiceImp and beans.xml file

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();
}

web service implementation:

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 file:

 <?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>

the problem is that when the getAllProducts() method is invoked in the service the nullpointer exception is thrown (property dao is null)

is there anything wrong with my code? Thanks for help

도움이 되었습니까?

해결책

Hi here is a solution that worked for me:

<?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" />

Thanks to all the participants

Best Regards

다른 팁

you can try to add @Autowired annotation in ProductWebServiceImp on your Dao.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top