Springboot 如何關(guān)閉自動配置
Springboot 關(guān)閉自動配置
springboot通過@SpringBootApplication 下的@EnableAutoConfiguration 實現(xiàn)自動配置,節(jié)約了開發(fā)者大量時間,但是有可能有些不必要的配置。如果想關(guān)閉其中的某一項配置,那應(yīng)該怎么辦呢?
使用@SpringBootApplication下的exclude參數(shù)即可。
舉例說明:
1. 關(guān)閉Redis自動配置
@SpringBootApplication(exclude={RedisAutoConfiguration.class })
2. SpringBoot默認(rèn)會自動配置數(shù)據(jù)庫
如果業(yè)務(wù)不需要 也可以可以在 pringBootApplication 注解中操作:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
注:有多項配置時可以用逗號隔開
開啟關(guān)閉自動任務(wù)配置流程
1.需求
可以根據(jù)自己配置的開關(guān),動態(tài)的控制springboot含有@Scheduled的定時任務(wù)
2.解決方案
1.刪除啟動類的 @EnableScheduling
2.利用condition進(jìn)行條件判斷
public class SchedulerCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return Boolean.valueOf(context.getEnvironment().getProperty("com.myapp.config.scheduler.enabled")); //就是yml值 } }
3.進(jìn)行新的定時任務(wù)裝配到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(); } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring自定義注解實現(xiàn)攔截器的實現(xiàn)方法
本篇文章主要介紹了spring自定義注解實現(xiàn)攔截器的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08詳細(xì)分析Java中String、StringBuffer、StringBuilder類的性能
在Java中,String類和StringBuffer類以及StringBuilder類都能用于創(chuàng)建字符串對象,而在分別操作這些對象時我們會發(fā)現(xiàn)JVM執(zhí)行它們的性能并不相同,下面我們就來詳細(xì)分析Java中String、StringBuffer、StringBuilder類的性能2016-05-05