spring.data.jpa.repositories.bootstrap-mode has been reverted to default since spring boot 2.4.1. I want to enable spring.data.jpa.repositories.bootstrap-mode=deferred but it is ignored (logs says DEFAULT)
However it is correctly enabled if instead of setting an application.propety, I set it with @EnableJpaRepositories("com.rebirthCorp.rebirth.repositories", bootstrapMode = BootstrapMode.DEFERRED)
Note: this might help you find the root cause of the bug:
Multiple Spring Data modules found, entering strict repository configuration mode!
@SpringBootApplication
@EnableIntegration
@EnableJpaRepositories("com.rebirthCorp.rebirth.repositories", bootstrapMode = BootstrapMode.DEFERRED)
@EnableElasticsearchRepositories("com.rebirthCorp.rebirth.elasticrepositories")
class RebirthApplication
Comment From: wilkinsona
We have a Data JPA smoke test that uses deferred mode configured in application.properties
and the configuration is applied:
2021-01-21 08:30:48.624 INFO 71765 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
I can see from the above that your app is using multiple Spring Data modules, beyond that it's not clear to me how it differs from an application where the configuration setting works. To avoid us having to guess about those differences when trying to recreate the problem, please spend some time providing a complete yet minimal sample that reproduces it. This will allow us to be sure that we're looking at the same problem as you. You can share a sample with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: wilkinsona
This also works for me with multiple Spring Data modules on the classpath:
2021-01-22 12:02:37.522 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-01-22 12:02:37.524 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Elasticsearch repositories in DEFAULT mode.
2021-01-22 12:02:37.531 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 4 ms. Found 0 Elasticsearch repository interfaces.
2021-01-22 12:02:37.535 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-01-22 12:02:37.536 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Reactive Elasticsearch repositories in DEFAULT mode.
2021-01-22 12:02:37.537 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 0 ms. Found 0 Reactive Elasticsearch repository interfaces.
2021-01-22 12:02:37.546 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-01-22 12:02:37.547 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2021-01-22 12:02:37.553 INFO 56986 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1 ms. Found 0 JPA repository interfaces.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: kolychev
I think I am having similar issue. If I use @EnableJpaRepositories annotation in production configuration (without bootStrap mode set in annotation itself) and spring.data.jpa.repositories.bootstrap-mode set in test application.properties, the value in properties is ignored and DEFAULT is used.
Sometimes I have to use EnableJpaRepositories just to configure where to search repositories for. However I did not set bootStrap in annotation because I want to use default value configured system-wide (in environment / application.properties). I want repositories to be initialized with DEFAULT mode in production, but in test I want repositories to be initialised lazily, so I set it in application.properties located in test folder. This does not work since AnnotationRepositoryConfigurationSource.getBootstrapMode does not take into account bootstrap-mode from application.properties and fallbacks to hardcoded BootstrapMode.DEFAULT.
I am attaching proof of concept project to reproduce this case.
In other words, DEFAULT mode from EnableJpaRepositories annotation (even if it was not explicitly declared) has higher priority over bootstrap-mode set in application.properties.
If this behaviour is implemented by design, could you please provide idiomatic way to set everything up?
Comment From: wilkinsona
In other words, DEFAULT mode from EnableJpaRepositories annotation (even if it was not explicitly declared) has higher priority over bootstrap-mode set in application.properties.
This is by design. If you use @EnableJpaRepositories
you are telling Spring Boot's auto-configuration to back off and indicating that you want to take control of things yourself.
If this behaviour is implemented by design, could you please provide idiomatic way to set everything up?
You should use the bootstrapMode
attribute on @EnableJpaRepositories
.
Comment From: kolychev
This is by design. If you use
@EnableJpaRepositories
you are telling Spring Boot's auto-configuration to back off and indicating that you want to take control of things yourself.
In my example I put JPA-related classes to package outside of main application package. So I need to scan repository interfaces somehow. That's why I use @EnableJpaRepositories
. Is there any other way to tell spring list of packages to scan repositories from ?
You should use the
bootstrapMode
attribute on@EnableJpaRepositories
.
Ok, then how do I suppose to configure DEFAULT bootstrapMode for production and LAZY mode for tests using same @EnableJpaRepositories
annotation ?
The only way I see now is to create different configurations for production and for test profiles like this:
@Configuration
@EnableJpaRepositories(bootstrapMode = DEFAULT, basePackageClasses = TestRepository.class)
@Profile("prod")
class ProdConfig {}
@Configuration
@EnableJpaRepositories(bootstrapMode = LAZY, basePackageClasses = TestRepository.class)
@Profile("test")
class TestConfig {}
But this seems odd to me. Much easier would be if I could use single configuration:
@Configuration
@EnableJpaRepositories(basePackageClasses = TestRepository.class)
class ProdConfig {}
By not explicitly declaring bootstrapMode at all, but could setup bootstrapMode from environment / application.properties. Does this behaviour breaks some case I don't see ? I don't mean to push in any way, just trying to understand how to solve this case in most idiomatic way.
Comment From: kolychev
Meanwhile for anyone landing this issue, I have a workaround to solve my issue. My original goal was to make all repository beans LAZY in tests. So I created small BeanFactoryPostProcessor that runs in tests only and makes all repository classes LAZY:
@Component
public class LazyRepositoryBFPP implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
for(var name : factory.getBeanNamesForType(Repository.class)) {
factory.getBeanDefinition(name).setLazyInit(true);
}
}
}
Comment From: wilkinsona
Is there any other way to tell spring list of packages to scan repositories from ?
As described in the documentation, you can use @EntityScan
for this purpose.
@kolychev If you have any further questions, please ask questions on Stack Overflow or Gitter. You're in a different situation to @LifeIsStrange so this isn't the place for this discussion. We also prefer to keep issues purely for bugs and enhancements.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
Comment From: rubensa
I had the same problem.
The key is that if you use @EnableJpaRepositories
annotation then Spring Boot auto-configuration is disabled.
I needed to use the annotation to configure repositoryBaseClass
. If you use the annotation, the value of bootstrapMode
is setted to BootstrapMode.DEFAULT
and the value spring.data.jpa.repositories.bootstrap-mode
is not taken into account.
I had to do something like this:
@Configuration
public class DataConfig {
private static final String REPOSITORY_PACKAGE = "org.eu.rubensa.repository";
@Value("${spring.data.jpa.repositories.bootstrap-mode:default}")
public static String SPRING_DATA_JPA_REPOSITORIES_BOOTSTRAP_MODE;
@Configuration
@ConditionalOnExpression("'${SPRING_DATA_JPA_REPOSITORIES_BOOTSTRAP_MODE}' == 'default'")
@EnableJpaRepositories(basePackages = {
REPOSITORY_PACKAGE }, repositoryBaseClass = CustomJpaRepository.class, bootstrapMode = BootstrapMode.DEFAULT)
public static class DefaultJpaRepositoriesConfig {
}
@Configuration
@ConditionalOnExpression("'${SPRING_DATA_JPA_REPOSITORIES_BOOTSTRAP_MODE}' == 'deferred'")
@EnableJpaRepositories(basePackages = {
REPOSITORY_PACKAGE }, repositoryBaseClass = CustomJpaRepository.class, bootstrapMode = BootstrapMode.DEFERRED)
public static class DeferredJpaRepositoriesConfig {
}
@Configuration
@ConditionalOnExpression("'${SPRING_DATA_JPA_REPOSITORIES_BOOTSTRAP_MODE}' == 'lazy'")
@EnableJpaRepositories(basePackages = {
REPOSITORY_PACKAGE }, repositoryBaseClass = CustomJpaRepository.class, bootstrapMode = BootstrapMode.LAZY)
public static class LazyJpaRepositoriesConfig {
}
}
Comment From: wilkinsona
@rubensa You can use @EntityScan
rather than @EnableJpaRepositories
to configure the base package.
Comment From: rubensa
@wilkinsona Can I specify repositoryBaseClass (that is the reason to use @EnableJpaRepository
) with @EntityScan anotation
? I don't think so... :(
Comment From: wilkinsona
Unfortunately not, no. It's store-specific so I don't think @EntityScan
would be the right place for it. That said, it might be nice if we could provide some other mechanism to do it so that you don't need to use @EnableJpaRepositories
. If that's something that you'd like us to explore, please open a new issue.
Comment From: Bas83
I had the same problem. Would prefer to configure through the properties rather than changing the mode in the @EnableJpaConfiguration annotation.