Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanning Spring Data repositories by Spring Config?

I'm trying to use spring data and spring config together in a small standalone application.

...
  public static void main( String[] args )
  {        
    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
    ... 
  }

1. My question is how can I discover the spring data repositories without using

<jpa:repositories base-package="foo.repositories" />

by spring config ?

2. If not, can I use 'ClassPathXmlApplicationContext' and 'AnnotationConfigApplicationContext' together somehow ?

like image 733
cscsaba Avatar asked Dec 22 '11 22:12

cscsaba


People also ask

What is @component scan configuration?

One of the most important annotations in spring is @ComponentScan which is used along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

What is difference between JpaRepository and CrudRepository?

Their main functions are: CrudRepository mainly provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sorting records. JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.

What is the @configuration in Spring boot?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.


1 Answers

You can now use the annotation @EnableJpaRepositories("some.root.package") .

For example:

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories("some.root.package")
@ComponentScan(basePackages = { "maybe.another.root.package" })
public class SystemConfiguration {
    ...
}

(Spring Data's announcement)

like image 168
Abdull Avatar answered Oct 06 '22 00:10

Abdull