From 46efed9f5ec40414f994bfe5f89354c3b39b56d8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 19 Mar 2013 17:27:03 +0100 Subject: [PATCH] DATAJPA-317 - Added @NoRepositoryBean to JpaRepository interface. This prevents the repository from being considered a repository a Spring bean instance has to be created for in case it is accidentally picked up during component scanning. --- .../data/jpa/repository/JpaRepository.java | 4 +++- .../JavaConfigUserRepositoryTests.java | 24 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java index 1d25e87d3..e51865be4 100644 --- a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2012 the original author or authors. + * Copyright 2008-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import java.io.Serializable; import java.util.List; import org.springframework.data.domain.Sort; +import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.PagingAndSortingRepository; /** @@ -26,6 +27,7 @@ import org.springframework.data.repository.PagingAndSortingRepository; * * @author Oliver Gierke */ +@NoRepositoryBean public interface JpaRepository extends PagingAndSortingRepository { /* diff --git a/src/test/java/org/springframework/data/jpa/repository/JavaConfigUserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/JavaConfigUserRepositoryTests.java index 43a93087f..d41a369a5 100644 --- a/src/test/java/org/springframework/data/jpa/repository/JavaConfigUserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/JavaConfigUserRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,14 +20,19 @@ import java.io.IOException; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import org.junit.Test; import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.PropertiesFactoryBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.core.io.ClassPathResource; import org.springframework.data.jpa.domain.sample.User; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.sample.UserRepository; import org.springframework.data.jpa.repository.sample.UserRepositoryImpl; import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean; @@ -76,4 +81,21 @@ public class JavaConfigUserRepositoryTests extends UserRepositoryTests { return new PropertiesBasedNamedQueries(factory.getObject()); } } + + /** + * @see DATAJPA-317 + */ + @Test(expected = NoSuchBeanDefinitionException.class) + public void doesNotPickUpJpaRepository() { + + ApplicationContext context = new AnnotationConfigApplicationContext(JpaRepositoryConfig.class); + context.getBean("jpaRepository"); + } + + @Configuration + @EnableJpaRepositories + @ImportResource("classpath:infrastructure.xml") + static class JpaRepositoryConfig { + + } }