2013-05-17 4 views
2

Spring documentation에 설명 된대로 사용자 정의 Java Spring JPA 저장소를 구현하려고합니다. 내 스프링 설정 나는 봄 3.2.2-RELEASE 및 스프링 데이터 jpa-를 사용하고 나에게Spring JPA 2.0 저장소/공장이 작동하지 않습니다.

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.myapp.repository.impl.DocumentRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.myapp.repository.impl.DocumentRepositoryImpl.<init>() 
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006) 
... 43 more 
Caused by: java.lang.NoSuchMethodException: com.myapp.repository.impl.DocumentRepositoryImpl.<init>() 
at java.lang.Class.getConstructor0(Class.java:2730) 
at java.lang.Class.getDeclaredConstructor(Class.java:2004) 
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) 

을주는 표준 방식으로 저장소를 생성하는 대신 주어진 MyRepositoryFactoryBean를 사용하여 주장 것 같다 1.3.2-RELEASE. 내가 맞으면 최신입니다.

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 

<import resource="spring-repository-config.xml"/> 
<import resource="spring-security-config.xml"/> 

<context:component-scan base-package="com.myapp.web.controller"/> 
<context:component-scan base-package="com.myapp.webservice.controller"/> 

을 그리고 여기에 스프링 저장소-config.xml 파일입니다 : 여기 내 스프링 설정입니다 If've이 COM의 모든 방법에서 디버그 중단 점을 추가

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/data/jpa" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"> 

<repositories base-package="com.myapp.repository" 
    factory-class="com.myapp.repository.impl.MyRepositoryFactoryBean"/> 
<!-- entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" --> 

. myapp.repository.impl.MyRepositoryFactoryBean 클래스이지만 이것들은 호출되지 않았습니다.

단지 예처럼 기본 인터페이스,

package com.myapp.repository.impl; 

@NoRepositoryBean 
public interface MyRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { 

기본 구현 :

package com.myapp.repository.impl; 

@NoRepositoryBean 
public class MyRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements MyRepository<T, ID> { 

private EntityManager entityManager; 

public MyRepositoryImpl(Class<T> domainClass, EntityManager entityManager) { 
    super(domainClass, entityManager); 

    // This is the recommended method for accessing inherited class dependencies. 
    this.entityManager = entityManager; 
} 

public void sharedCustomMethod(ID id) { 
    // implementation goes here 
} 
} 

그리고 공장 :

을 :

package com.myapp.repository.impl; 

import java.io.Serializable; 

import javax.persistence.EntityManager; 

import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; 
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean; 
import org.springframework.data.repository.core.RepositoryMetadata; 
import org.springframework.data.repository.core.support.RepositoryFactorySupport; 

public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> { 

protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { 
    return new MyRepositoryFactory(entityManager); 
} 

private static class MyRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory { 
    private EntityManager entityManager; 

    public MyRepositoryFactory(EntityManager entityManager) { 
     super(entityManager); 
     this.entityManager = entityManager; 
    } 

    protected Object getTargetRepository(RepositoryMetadata metadata) { 
     return new MyRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager); 
    } 

    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { 
     // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory 
     // to check for QueryDslJpaRepository's which is out of scope. 
     return MyRepositoryImpl.class; 
    } 
} 
} 

내 저장소 인터페이스로 정의된다

그리고 구현

package com.myapp.repository.impl; 

import java.io.Serializable; 

import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 

public class DocumentRepositoryImpl<Document, ID extends Serializable> extends MyRepositoryImpl<Document, Serializable> { 

    private static final long serialVersionUID = 1L; 

     public DocumentRepositoryImpl(Class<Document> domainClass, EntityManager entityManager) { 
     super(domainClass, entityManager); 
    } 

만 내 컨트롤러에서 autowire가 refernces 이러한 저장소를 사용 : 나는 this one 같은 웹에서 다양한 자원을 검토 한

package com.myapp.web.controller; 

@Controller 
@RequestMapping(value = "/documents") 
public class DocumentController { 

@Autowired 
private DocumentRepository documentRepository; 

@RequestMapping(method = RequestMethod.GET) 
public ModelAndView list(HttpServletRequest request) { 
    this.documentRepository. ... 
} 

,하지만 난 할 수 내 코드와의 차이점을 말하지 마라. 어떤 힌트라도 환영합니다! com.myapp.repository.impl.DocumentRepositoryImpl

public DocumentRepositoryImpl(){} 

봄 먼저 기본 생성자 (매개 변수없이)를 호출하여 당신이 (귀하의 경우) 응용 프로그램 컨텍스트에서 선언 콩을 인스턴스화하기위한

+0

이 문제가 해결 되었습니까? –

답변

0

당신은 기본 생성자를 필요보다 다른 주입하는 세터를 사용 콩.

+0

당신의 빠른 대답을위한 Thx. 그러나 부모 클래스 인 SimpleJpaRepository에는 2 개의 생성자가 있으며 각 인수에는 2 개의 인수가 제공되어야합니다. [어설 션 실패] -이 인수는 필수입니다. null이 아니어도된다. org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata (JpaEntityInformationSupport.java:58) at org.springframework.data.jpa.repository.support.SimpleJpaRepository. (SimpleJpaRepository.java:91) at com.myapp.repository.impl.MyRepositoryImpl. (MyRepositoryImpl.java:18) at com.myapp.repository.impl.DocumentRepositoryImpl. Ronald

+1

그리고 여기 PersonRepoImpl 클래스 (http://hvanhove.blogspot.nl/2013/03/generic-repo-for-all-repositories.html)에는 기본 생성자도 없습니다. – Ronald

관련 문제