spring boot 学习9-@Conditional

@Conditional注解是帮助我们更加方便的去创建bean,类似于@Profile.这里学习一个例子来来展示@Conditional注解如何帮助我们更加灵活的创建bean.

首先设定两个条件,当符合这两个条件时我们分别创建不同的bean

判定程序是否运行在weindows 环境

/**
 * Created by lengshan on 2018/4/10 0010.
 */
public class WindowsCondition implements Condition {

    //获取系统的环境变量
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().getProperty("os.name").contains("Windows");
    }
}

判定程序是否运行在linux环境中

/**
 * Created by lengshan on 2018/4/10 0010.
 */
public class LinuxCondition implements Condition {


    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().getProperty("os.name").contains("Linux");
    }
}

创建一个接口,并且分别创建实现这个接口的两个实现类,后面我们根据前面的两个条件来判断容器调用哪个实现类去创建bean

创建接口

/**
 * Created by lengshan on 2018/4/10 0010.
 */
public interface ListService {

    public String showListService();
}

创建实现类

/**
 * Created by Administrator on 2018/4/10 0010.
 */
public class LinuxListService implements ListService {
    @Override
    public String showListService() {
        return "ls";
    }
}
/**
 * Created by Administrator on 2018/4/10 0010.
 */
public class WindowsListService implements ListService {
    @Override
    public String showListService() {
        return "dir";
    }
}

最重要的编写配置文件,这里具体应该@Conditional注解来帮助我们判断去创建哪个bean

/**
 * Created by lengshan on 2018/4/10 0010.
 */
@Configuration
//@ComponentScan("com.example.demo")
public class MyConfig {
    @Bean
    @Conditional(WindowsCondition.class)
    public ListService windowsListService(){
        return new WindowsListService();
    }

    @Bean
    @Conditional(LinuxCondition.class)
    public ListService linusListService(){
        return new LinuxListService();
    }

}

编写main,并输出返回值。当程序运行在linux下时返回ls,运行在windows下时返回dir

@SpringBootApplication
public class DemoSpringcongditionalApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoSpringcongditionalApplication.class, args);
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);

		ListService listService = context.getBean(ListService.class);
		System.out.println(listService.showListService());

		context.close();

	}
}
参考:《Java EE开发的颠覆者 spring boot实战》

Spring Boot中,@Conditional注解和@AutoConfigureAfter注解是非常常用的注解,下面我来给你详细解析一下这两个注解。 ## @Conditional注解 @Conditional注解是Spring Boot中非常重要的一个注解,在Spring Boot中,很多自动配置都是通过@Conditional注解来实现的。 @Conditional注解可以根据满足某些条件来决定是否创建一个bean。比如,我们可以根据某个类是否存在来决定是否创建一个bean,具体示例如下: ```java @Configuration @Conditional(ExistClassCondition.class) public class MyConfiguration { @Bean public MyBean myBean() { return new MyBean(); } } public class ExistClassCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { try { Class.forName("com.example.MyClass"); return true; } catch (ClassNotFoundException e) { return false; } } } ``` 上面的代码中,我们定义了一个MyConfiguration类,并且在该类上加了@Conditional注解,该注解的参数是一个Condition的实现类ExistClassCondition。ExistClassCondition类中的matches方法返回true的条件是com.example.MyClass类存在。 这样,当com.example.MyClass类存在的时候,MyBean这个bean才会被创建。否则,MyBean这个bean不会被创建。 ## @AutoConfigureAfter注解 @AutoConfigureAfter注解也是Spring Boot中比较常用的注解之一,它可以用来控制自动配置的顺序。 比如,我们可以通过@AutoConfigureAfter注解来控制某个自动配置类在另一个自动配置类之后加载,具体示例如下: ```java @Configuration @AutoConfigureAfter(MyAutoConfiguration.class) public class MyAnotherAutoConfiguration { // ... } ``` 上面的代码中,我们定义了一个MyAnotherAutoConfiguration类,并且在该类上加了@AutoConfigureAfter注解,该注解的参数是MyAutoConfiguration.class。这样,在Spring Boot启动时,MyAutoConfiguration这个自动配置类会先于MyAnotherAutoConfiguration这个自动配置类被加载。 总结:@Conditional注解和@AutoConfigureAfter注解都是Spring Boot中非常实用的注解。通过@Conditional注解可以实现根据满足某些条件来决定是否创建一个bean,通过@AutoConfigureAfter注解可以控制自动配置类的加载顺序,这些都是我们在实际开发中非常常用的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值