Springboot How to Turn off Automatic Configuration
- 2021-11-13 01:27:09
- OfStack
Springboot Turn off Automatic Configuration
springboot is automatically configured via @ EnableAutoConfiguration under @ SpringBootApplication, which saves developers a lot of time, but there may be some unnecessary configuration. What should I do if I want to turn off one of the configurations?
Use the exclude parameter under @ SpringBootApplication.
For example:
1. Turn off Redis automatic configuration
@SpringBootApplication(exclude={RedisAutoConfiguration.class })
2. SpringBoot automatically configures the database by default
You can also operate in pringBootApplication annotations if the business does not need it:
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
Note: Multiple configurations can be separated by commas
Turn on and off the automatic task configuration process
1. Requirements
You can dynamically control the timing task of springboot with @ Scheduled according to the switch configured by yourself
2. Solutions
1. Delete @ EnableScheduling for the startup class
2. Condition judgment using condition
public class SchedulerCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return Boolean.valueOf(context.getEnvironment().getProperty("com.myapp.config.scheduler.enabled")); // Is yml Value
}
}
3. Assemble new timed tasks to IOC
@Configuration
public class Scheduler {
@Conditional(SchedulerCondition.class)
@Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
return new ScheduledAnnotationBeanPostProcessor();
}
}