一千萬個為什麽

搜索

Spring 3.0中的@ContextConfiguration給我找不到默認構造函數

我已經使用AbstractDependencyInjectionSpringContextTests進行測試並且它可以工作但是在春季3它已被棄用,所以我決定嘗試@ContextConfiguration但是春天說沒有找到默認構造函數,我檢查並且該類沒有任何構造函數。

如果我使用這個測試彈簧給出物體。

package atoms.portales.servicios.impl;

import atoms.portales.model.Cliente;
import atoms.portales.servicios.ClienteService;
import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;


/**
 *
 * @author tsalazar
 */

    public class ClienteServiceImplDeTest extends AbstractDependencyInjectionSpringContextTests{


        private ClienteService clienteService;

        public ClienteService getClienteService() {
            return clienteService;
        }

        public void setClienteService(ClienteService clienteService) {
            this.clienteService = clienteService;
        }




        public ClienteServiceImplDeTest(String testName) {
            super(testName);
        }


        @Override
        protected String[] getConfigLocations() {
            return new String[]{"PersistenceAppCtx.xml", "ServicesAppCtx.xml"};
        }



        /**
        * Test of buscaCliente method, of class ClienteServiceImplDeTest.     */
        public void testBuscaCliente() {
            System.out.println("=======================================");
            System.out.println("buscaCliente");
            String nombre = "";

              System.out.println(clienteService);


             System.out.println("=======================================");



        }


    }

但是,如果我使用它,spring說沒有找到默認構造函數。

package atoms.config.portales.servicios.impl;

import atoms.portales.model.Cliente;
import atoms.portales.servicios.ClienteService;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author tsalazar
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/PersistenceAppCtx.xml", "/ServicesAppCtx.xml"})
@TransactionConfiguration(transactionManager = "transactionManager")
@Transactional
public class ClienteServiceImplTest {

    @Autowired
    private ClienteService clienteService;

    /**
     * Test of buscaCliente method, of class ClienteServiceImpl.     */
    @Test
    public void testBuscaCliente() {
        System.out.println("=======================================");
        System.out.println("buscaCliente");


        System.out.println(clienteService);



        System.out.println("=======================================");



    }
}

這是我如何做的實現:

package atoms.portales.servicios;

import atoms.portales.model;

    /**
     * Una interface para obtener clientes, con sus surcursales, servicios, layouts
     * y contratos. Tambien soporta operaciones CRUD.
     * @author tsalazar
     */
    public interface ClienteService {

        /**
         * Busca clientes a partir del nombre
         * @param nombre
         */
         public Cliente buscaCliente(String nombre);



    }

實施

package atoms.portales..servicios.impl;

import atoms.portales.model.Cliente;
import atoms.portales.servicios.ClienteService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * A JPA-based implementation.Delegates to a JPA entity manager to issue data access calls
 * against the backing repository. The EntityManager reference is provided by the managing container (Spring)
 * automatically.
 */
@Service("clienteSerivice")
@Repository
public class ClienteServiceImpl implements ClienteService {

    public ClienteServiceImpl() {
    }


    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    @Transactional(readOnly = true)
    public Cliente buscaCliente(String nombre) {


        Cliente cliente = em.getReference(Cliente.class, 1l);
        return cliente;

    }


}

彈簧配置:

<?xml version="1.0" encoding="UTF-8"?>




<!-- Instructs Spring to perfrom declarative transaction management on annotated classes -->
    

    <!-- Drives transactions using local JPA APIs -->
    
        
    

    <!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data -->
    
        
        
            
        
    

    <!-- Deploys a in-memory "booking" datasource populated -->
    
        
        
        
        
    







這是persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

  
       org.hibernate.ejb.HibernatePersistence
    atoms.portales.model.Cliente
    
      
      
      
      
    
  

這是給我的錯誤:

最佳答案

對不起,我註意到wath正在發生,當我進行測試時,我將ClienteServiceImplDeTest的名稱更改為ClienteServiceImpl,因為我只想檢查新測試,但這會幹擾服務實現。我在同一個包中有兩個同名的類就是這樣。

轉載註明原文: Spring 3.0中的@ContextConfiguration給我找不到默認構造函數