spring jpa data没有默认构造函数

时间:2015-01-14 03:19:09

标签: spring spring-data spring-data-jpa

我使用spring jpa数据,扩展JpaRepository,现在我想添加新方法, 代码是

public interface HighWayRepository extends JpaRepository<HighWay, String> {

    void updateNewHighWay(List<HighWay> mockedList);

}

public class HighWayRepositoryImpl extends SimpleJpaRepository<HighWay, String>
        implements HighWayRepository {

    private EntityManager entityManager;

    public HighWayRepositoryImpl(Class<HighWay> domainClass, EntityManager em) {
        super(domainClass, em);
        this.entityManager = em;
    }

    @Override
    public void updateNewHighWay(List<HighWay> mockedList) {

    }

}

我使用junit test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:conf/context.xml" })
@Component
@TransactionConfiguration(defaultRollback = false)
@Transactional
public class HttpTest {
    @Resource
    private HighWayRepository repository;
     @test
     ......}

但它会抛出异常

  

java.lang.IllegalStateException:无法加载ApplicationContext   org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)     在   org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)     在   org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)     在   org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)     在   org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:252)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:254)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)at at   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)     在org.junit.runners.ParentRunner.run(ParentRunner.java:309)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:org.springframework.beans.factory.BeanCreationException:   创建名称为&#39; httpTest&#39;:注入资源的bean时出错   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; highWayRepositoryImpl&#39;的bean在文件中定义   [d:\ WYR \ chinahighway \公路\目标\类\ chinahighway \库\ IMPL \ HighWayRepositoryImpl.class]:   bean的实例化失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例化[chinahighway.repository.impl.HighWayRepositoryImpl]:不   找到默认构造函数;嵌套异常是   java.lang.NoSuchMethodException:   chinahighway.repository.impl.HighWayRepositoryImpl。()at   org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)     在   org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)     在   org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)     在   org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)     在   org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)     在   org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)     在   org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)     ... 25更多引起:   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; highWayRepositoryImpl&#39;的bean在文件中定义   [d:\ WYR \ chinahighway \公路\目标\类\ chinahighway \库\ IMPL \ HighWayRepositoryImpl.class]:   bean的实例化失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例化[chinahighway.repository.impl.HighWayRepositoryImpl]:不   找到默认构造函数;嵌套异常是   java.lang.NoSuchMethodException:   chinahighway.repository.impl.HighWayRepositoryImpl。()at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1093)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1038)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)     在   org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:457)     在   org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:435)     在   org.springframework.context.annotation.CommonAnnotationBeanPostProcessor $ ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:559)     在   org.springframework.beans.factory.annotation.InjectionMetadata $ InjectedElement.inject(InjectionMetadata.java:169)     在   org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)     在   org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:305)     ... 41更多引起:   org.springframework.beans.BeanInstantiationException:失败   实例化[chinahighway.repository.impl.HighWayRepositoryImpl]:不   找到默认构造函数;嵌套异常是   java.lang.NoSuchMethodException:   chinahighway.repository.impl.HighWayRepositoryImpl。()at   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1086)     ... 57更多引起:java.lang.NoSuchMethodException:   chinahighway.repository.impl.HighWayRepositoryImpl。()at   java.lang.Class.getConstructor0(Class.java:2892)at   java.lang.Class.getDeclaredConstructor(Class.java:2058)at   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)     ......还有58个

我谷歌异常,原因是HighWayRepositoryImpl类没有参数构造函数,但我无法添加它。 怎么可以正常运行测试?或者可能添加配置和注释?

请帮助我,谢谢你的帮助。

0 个答案:

没有答案