hualinux spring 4.8:@Conditional按条件注入

目录

一、知识点

二、例子

2.1 目录结构

2.2 实现代码

2.3 运行


@Conditional 按条件注入

一、知识点

注入bean时,指定一些条件@Conditional,标记在配置类或bean方法上

 

二、例子

废话小少说了,直接上例子,我这里使用的是《hualinux spring 4.3:idea 2020创建简单的spring注解式》 建立的项目为基础的

例子的功能是根据系统动态输出

2.1 目录结构

2.2 实现代码

src-->com.hualinux.spring.WindowsConditon.java

package conf;

import com.hualinux.spring.Hello;
import com.hualinux.spring.LinuxConditon;
import com.hualinux.spring.WindowsConditon;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;


//告诉spring 这是一个配置文件,这个注解是一定要有的
@Configuration
public class HelloConf {



    @Conditional({WindowsConditon.class})
    @Bean("bill")
    public Hello hello1(){
        return new Hello("win");
    }

    @Conditional({LinuxConditon.class})
    @Bean("linus")
    public Hello hello2(){
        return new Hello("linux");
    }
    
}

com.hualinux.spring.LinuxConditon.java

package com.hualinux.spring;

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;

public class LinuxConditon implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //获取IOC的beanFactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        //获取当前环境信息
        Environment environment = context.getEnvironment();
        //获取到bean的定义注册类
        BeanDefinitionRegistry registry = context.getRegistry();
        String property = environment.getProperty("os.name");
        if(property.contains("linux")){
            return true;
        }
        return false;
    }
}

conf.HelloConf.java 配置如下

package conf;

import com.hualinux.spring.Hello;
import com.hualinux.spring.LinuxConditon;
import com.hualinux.spring.WindowsConditon;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;


//告诉spring 这是一个配置文件,这个注解是一定要有的
@Configuration
public class HelloConf {



    @Conditional({WindowsConditon.class})
    @Bean("bill")
    public Hello hello1(){
        return new Hello("win");
    }

    @Conditional({LinuxConditon.class})
    @Bean("linus")
    public Hello hello2(){
        return new Hello("linux");
    }
    
}

com.hualinux.spring.HelloMain.java配置如下:

package com.hualinux.spring;

import conf.HelloConf;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class HelloMain {
    public static void main(String[] args) {
        //1. 创建 Spring 的IOC容器对象
        //这次使用的不是ClassPathXmlApplicationContext类了,而是注解类
        AnnotationConfigApplicationContextctx= new AnnotationConfigApplicationContext(HelloConf.class);
        ConfigurableEnvironment environment= (ConfigurableEnvironment) ctx.getEnvironment();
        //动态获取环境变量的值,Windows
        String property = environment.getProperty("os.name");
        System.out.println(property);

        String[] beanNamesForType = ctx.getBeanNamesForType(Hello.class);
        for (String name : beanNamesForType){
            System.out.println(name);
        }


        //2. 从 IOC 容器中获取 Bean 实例,填写Bean的ID就是java配置文件的方法名
        //Hello hello= (Hello) ctx.getBean("hello");

        //3. 调用sayHello方法
        //hello.sayHello();

    }
}

2.3 运行

运行上面的.HelloMain.java,结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值