gpt4 book ai didi

java - Spring 3.0 中的 @ContextConfiguration 给我找不到默认构造函数

转载 作者:行者123 更新时间:2023-12-01 05:56:33 25 4
gpt4 key购买 nike

我已经使用 AbstractDependencyInjectionSpringContextTests 进行了测试,它可以工作,但在 spring 3 中它已被弃用,所以我决定尝试 @ContextConfiguration 但 spring 说找不到默认构造函数,我检查并且该类没有任何构造函数.

如果我使用这个测试 Spring ,给出该对象。

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("=======================================");



}
}

这就是我的实现方式:

包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;

}


}

Spring 配置:

<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">



<!-- Instructs Spring to perfrom declarative transaction management on annotated classes -->
<tx:annotation-driven />

<!-- Drives transactions using local JPA APIs -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>

<!-- Deploys a in-memory "booking" datasource populated -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/test" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>


<context:component-scan base-package="atoms.portales.servicios" />



</beans>

这是 persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="configuradorPortales" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>atoms.portales.model.Cliente</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
</properties>
</persistence-unit>
</persistence>

这是给我的错误:

最佳答案

抱歉,我留意正在发生的情况,当我进行测试时,我将 ClienteServiceImplDeTest 的名称更改为 ClienteServiceImpl 因为我只想检查新测试,但这会干扰服务实现。我在同一个包中有两个同名的类,这就是发生的情况。

关于java - Spring 3.0 中的 @ContextConfiguration 给我找不到默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2782763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com