栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

用@ConditionalOnMissingBean实现覆盖第三方组件中的bean

用@ConditionalOnMissingBean实现覆盖第三方组件中的bean

如下制作一个第三方组件:

@Configuration
public class MyfgConfig {

    @Bean
    @ConditionalOnMissingBean
    public Myfg getMyfg(){
        return new Myfg();
    }
}
public class Myfg {
    public void say(){
        System.out.println("-------------1----------");
    }
}


	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.2.2.RELEASE
		
	
	com.example
	demo
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	



注意这里不要引入springboot的插件,用maven原生的,否则使用maven  install的时候,其他工程引入不进去

 在另外一个工程中maven引入:

        
            com.example
            demo
            0.0.1-SNAPSHOT
        

用import注解注入到此spring容器中,启动之后,看打印日志是:

我们再添加两个类:

@Configuration
public class MongodbConfig {

    @Bean
    public Myfg getMongoTemplate(){
        return new MongoTemplateCz();
    }

}
public class MongoTemplateCz extends Myfg {

    @Override
    public void say() {
        System.out.println("-------------2----------");
    }
}

再次启动,看打印日志:

至此,完美覆盖了 第三方组件的bean ,所以@ConditionalOnMissingBean提高了第三方组件的可扩展性

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/734381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号