组件注册-@Conditional-按照条件注册bean

@Conditional:满足条件才会给容器中注册bean

    @Bean
    @Conditional({WindowsCondition.class})
    public Person person01() {
        System.out.println("person01 被创建");
        return new Person(60, "Bill Gates");
    }
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        Environment environment = context.getEnvironment();

        String s = environment.getProperty("os.name");
        System.out.println(s);
        if (s.contains("Windows")) {
            return true;
        }
        return false;
    }
Windows 7
person01 被创建

 

posted on 2019-05-23 16:29  星辰钟塔的玛利亚女士  阅读(118)  评论(0编辑  收藏  举报

导航