I was finally able to achieve all the required filtering, thanks to Josef's answer and these:
Components and Services can be configured to yield filters hence we can specify our target services and controllers and exclude everything else at the same time:
@ComponentScan(
basePackageClasses = {
MyTargetService.class,
MyTargetController.class
},
useDefaultFilters = false,
includeFilters = {
@ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetService.class),
@ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetController.class)
}
)
Repositories. This is unlikely to work for repositories, but fortunately the @EnableJpaRepositories
supports the same type of filters:
@EnableJpaRepositories(
basePackageClasses = {
MyTargetRepository.class
},
includeFilters = {
@ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetRepository.class)
}
)
Entities. This part is more tricky because @EntityScan does not support these filters. Although the entities do not reference Spring beans, I prefer loading only the entities necessary for my test. I was not able to find any annotation for entities that supports filtering, but we can filter them programmatically using a PersistenceUnitPostProcessor
in our EntityManagerFactory
. Here is my full solution:
//add also the filtered @ComponentScan and @EnableJpaRepositories annotations here
@Configuration
public class MyConfig {
//here we specify the packages of our target entities
private static String[] MODEL_PACKAGES = {
"com.full.path.to.entity.package1",
"com.full.path.to.entity.package2"
};
//here we specify our target entities
private static Set<String> TARGET_ENTITIES = new HashSet<>(Arrays.asList(
"com.full.path.to.entity.package1.MyTargetEntity1",
"com.full.path.to.entity.package2.MyTargetEntity2"
));
@Bean
public DataSource getDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
ReflectionsPersistenceUnitPostProcessor reflectionsPersistenceUnitPostProcessor = new ReflectionsPersistenceUnitPostProcessor();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(MODEL_PACKAGES);
factory.setDataSource(getDataSource());
factory.setPersistenceUnitPostProcessors(reflectionsPersistenceUnitPostProcessor);
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
public class ReflectionsPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {
@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
Reflections r = new Reflections("", new TypeAnnotationsScanner());
Set<Class<?>> entityClasses = r.getTypesAnnotatedWith(Entity.class, true);
Set<Class<?>> mappedSuperClasses = r.getTypesAnnotatedWith(MappedSuperclass.class, true);
pui.getManagedClassNames().clear(); //here we remove all entities
//here we add only the ones we are targeting
for (Class<?> clzz : mappedSuperClasses) {
if (TARGET_ENTITIES.contains(clzz.getName())) {
pui.addManagedClassName(clzz.getName());
}
}
for (Class<?> clzz : entityClasses) {
if (TARGET_ENTITIES.contains(clzz.getName())) {
pui.addManagedClassName(clzz.getName());
}
}
}
}
}