@Configuration注解

Config类
package com.atguigu.config;

import com.atguigu.bean.Person;
import com.atguigu.condition.Liunx;
import com.atguigu.condition.Windows;
import org.springframework.context.annotation.*;

// @Conditional({Windows.class})
// 类中组件统一设置。满足当前条件,这个类中配置的所有bean才会生效
@Configuration  //表示配置文件类
public class MyConfig2 {


    /*
    * @Conditional:按照一定的条件判断,满足条件的给容器中注册bean
    *
    *
    * 如果是windows系统,给容器注册("bill")
    * 如果是linux系统,给容器注册("linus")
    * */

    @Conditional({Windows.class})
    @Bean("bill")
    public Person person01(){
        return new Person("Bill Gates",62);
    }

    @Conditional({Liunx.class})
    @Bean("linus")
    public Person person02(){
        return new Person("linus",48);
    }
}

Condition类

liunx
package com.atguigu.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

//判断是否wlinux系统
public class Liunx  implements Condition {

    /*
    * conditionContext:判断条件能使用的上下文(环境)
    * AnnotatedTypeMetadata:注释信息
    * */
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        // 判断是否liunx系统
        //1.获取到ioc使用的BeanFactory
        ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
        //2.获取类加载器
        ClassLoader classLoader = conditionContext.getClassLoader();
        //3.获取当前环境信息
        Environment environment = conditionContext.getEnvironment();
        //4.获取到bean定义的注册类
        BeanDefinitionRegistry registry = conditionContext.getRegistry();
        //判断操作系统
        String property = environment.getProperty("os.name");

        //可以判断容器中的bean的注册情况,也可以给容器注册bean
        boolean person = registry.containsBeanDefinition("person");

        if(property.contains("linux")){
            return true;
        }
        return false;
    }
}

windows
package com.atguigu.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

//判断是否windows系统
public class Windows implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        // 判断是否Windows系统
        //1.获取到ioc使用的BeanFactory
        ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
        //2.获取类加载器
        ClassLoader classLoader = conditionContext.getClassLoader();
        //3.获取当前环境信息
        Environment environment = conditionContext.getEnvironment();
        //4.获取到bean定义的注册类
        BeanDefinitionRegistry registry = conditionContext.getRegistry();
        //判断操作系统
        String property = environment.getProperty("os.name");
        if(property.contains("Windows")){
            return true;
        }
        return false;
    }
}

Test类
@Test
    public void Test03(){
        String[] names = context.getBeanNamesForType(Person.class);
        ConfigurableEnvironment environment = context.getEnvironment();
        String property = environment.getProperty("os.name");
        System.out.println("操作系统"+property);
        for (String name : names) {
            System.out.println(name);
        }

        Map<String, Person> map = context.getBeansOfType(Person.class);
        System.out.println(map);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值