springboot2笔记,底层注解,@Conditional条件装配

条件装配,满足Conditional指定的条件,则进行组件注入

springboot2笔记,底层注解,@Conditional条件装配

如@ConditionalOnBean

package com.gotham.boot.config;


import ch.qos.logback.core.db.DBHelper;
import com.gotham.boot.bean.Pet;
import com.gotham.boot.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods=false)
public class MyConfig {

    @Bean("bat")
    public Pet pet01(){
        return new Pet("1","2");
    }

    //表示含有名字为bat的bean才会注册该组件,注意编译顺序
    @ConditionalOnBean(name="bat")
    @Bean
    public User user01(){
        return new User("1","2","3",pet01());
    }

}

springboot2笔记,底层注解,@Conditional条件装配

甚至可以标注到类上,如满足条件该类的所有组件才会生效

上一篇:springboot2.x-@Conditional条件装配


下一篇:【SpringBoot】自动配置原理 2 HttpEncodingAutoConfiguration详解