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 ?
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With