8

I'm woundering if it's possible to disable the intialization of JPA Stack competelly.

I some situation my application is executed in enviroment where database is not accessible. Application can survive this on application level. But technically i get some exception on intialization of Data Source.

  1. there musst be some way to disable JPA intialization at all (not needed withoud db. By profile or someting like that.
  2. Or maby at least i can say Connection pool to not connect or somethinglike this.
  3. Other ideas?
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
aholbreich
  • 4,951
  • 2
  • 23
  • 38

1 Answers1

9

Yes, you could use profiles for that: one with JPA and another without it. To turn off DataSource/JPA autoconfiguration you need to add spring.autoconfigure.excludes property (with fully-qualified class names) to profile-specific application.properties.

What classes to exclude? I suggest to start from DataSourceAutoConfiguration and if it's not enough try to disable other database-related autoconfiguration classes.

Note that spring.autoconfigure.excludes is a new feature that is avaiable since Spring Boot 1.3.0.M3

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
  • Yes, but how to make that "eclusion" **conditionaly** to Profile. And Secondly: How then to deal with _Autowired_. Would mean in need custom factories? Maybe some exmaple helps me – aholbreich Sep 16 '15 at 07:21
  • Ok Autowirede can have Required=false... Now i hvae to figure out how to make EnableAutoConfiguration(exclude = DataSourceAutoConfiguration .class) conditional, depending on Profile – aholbreich Sep 16 '15 at 08:17
  • 1
    `spring.autoconfigure.excludes` is analogue of `EnableAutoConfiguration(exclude = )` that may be used in `application.properties`. Just put it in `application-nodb.properties` file where `nodb` is a name of profile. – Slava Semushin Sep 16 '15 at 08:19
  • Here i try to exclude DataSourceAutoconfig, (So it not tries to establish connection) spring.autoconfigure.excludes=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration. But it's not working, no chnage to behavior. Tried with DataSourceAutoConfiguration too... Spring boot 1.3.0.M5 – aholbreich Sep 16 '15 at 09:59
  • Run application with `--debug`: has it shown as enabled? – Slava Semushin Sep 16 '15 at 10:45
  • It's not shown as disabled. Exclusions: ----------- org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration and this is excluded non conditionally – aholbreich Sep 16 '15 at 11:52
  • I've solved a litte bitt differentlyy so far. WIt expilicite configuration class @Profile(value = {"dev"}) @Configuration @ImportAutoConfiguration({DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class JPAConfig { private static final Logger LOG = LoggerFactory.getLogger(AppCommandLinstener.class); _PostConstruct public void logStatus() { LOG.info("======== DB Configuration initialized ========="); } } and explicitely disabling it in Application.java – aholbreich Sep 16 '15 at 16:21
  • But the problem is it depends on environment not on the property within environment. but it's fine.. – aholbreich Sep 16 '15 at 16:24
  • I came across this post after looking for a spring boot data source initialization problem and had somewhat the same issue (in my case we have multiple datasources, which caused a failure). In the end I had to add a property to my spring properties: `spring.datasource.initialize=false`. This besides disabling the auto configuration. – DelGurth Jan 13 '17 at 17:36