Some automatic configuration classes are ignored when SpringBoot starts

Recently, services require data interconnection, and you need to connect to a third-party database. But they are network, can only through the front on the deployment of our project, because of our dock with the third party processing are written in a single service, if a third party, alone to write a service will be very trouble, bad management, so you need to write all the processing in the existing service, but there is a problem that is: This time, we need to put the service on the specified front-end machine. Originally, we used multiple data sources to connect to the third-party database, and the connected mysql and Redis will be registered with Eureka. When we put the service directly on the front-end machine, errors such as Redis and Eureka cannot be found will be reported. So you need to ignore Spring’s two auto-configuration classes

The solution

Add to the startup class:

@SpringBootApplication(exclude = {EurekaClientAutoConfiguration.class, RedisAutoConfiguration.class})
Copy the code

Problem: Every time you bootstrap it to the front, you need to change the code and repackage it

The only annotation that actually works is “EnableAutoConfiguration”, so if you go to EnableAutoConfiguration, you can see the Spring instructions for this class.

As you can see, Spring startup is ignored in two ways:

1, exclude specified in the @ SpringBootApplication annotations on 2, in the spring. The specified in the configuration file autoconfigure. Exclude

The project originally used different configuration files for different environments. Using the second method can save the need to change the code during deployment.

spring:
  autoconfigure:
    exclude:
      - org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration
      - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
Copy the code