Spring Boot @Conditional相关注解解析

Posted 咩咩文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot @Conditional相关注解解析相关的知识,希望对你有一定的参考价值。

1.配置类

package com.spring4all.convert;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import com.spring4all.domain.User;
/**
 * 条件配置类
 * @author Bowin
 *
 */
@Configuration
public class EncodingConvertConfiguration 

	/**
	 * 只有满足Wind8Condition条件之后创建一个bean。
	 * @return
	 */
	@Bean
	@Conditional(WindowsCondition.class)
	public EncodingConvert windowsEncodingConvert()
		System.out.println("===开始装配:@Conditional===");
		return new WindowsEncodingConvert();
	
	
	@Bean
	@Conditional(LinuxCondition.class)
	public EncodingConvert LinuxEncodingConvert()
		System.out.println("===开始装配:@Conditional===");
		return new LinuxEncodingConvert();
	
	
	/**
	 * @ConditionalOnProperty 表示classpath下有某个属性的时候才装配
	 * @return
	 */
	@Bean 
	@ConditionalOnProperty(name="runnable-enable",havingValue="false",matchIfMissing=false)
	public Runnable createRunable()
		System.out.println("===开始装配:@ConditionalOnProperty===");
		return ()->;
		
	
	/**
	 * @ConditionalOnClass 表示classpath下有某个class的时候才装配
	 * @return
	 */
	@Bean 
	@ConditionalOnClass(EncodingConvertConfiguration.class)
	public Runnable createRunable2()
		System.out.println("===开始装配:@ConditionalOnClass===");
		return ()->;
		
	
	/**
	 *SpEL表达式结果为true时才装配
	 * @return
	 */
	@Bean
	@ConditionalOnExpression("$enabled:false")
	public Runnable createRunnable3()
		System.out.println("===开始装配:@ConditionalOnExpression===");
		return ()->;
	
	/**
	 * @ConditionalOnBean 表示class下存在某个bean的时候装配
	 * @ConditionalOnMissBean 表示class下不存在某个bean的时候装配
	 * @return
	 */
	@Bean 
	@ConditionalOnBean(User.class)
	public User createUserBean()
		System.out.println("===开始装配:@ConditionalOnBean===");
		return new User("a");
		
	
	

2.接口类
package com.spring4all.convert;
/**
 * 装换接口
 * @author Bowin
 *
 */
public interface EncodingConvert

	public  void print();

3.实现类

package com.spring4all.convert;

public class WindowsEncodingConvert  implements EncodingConvert

	@Override
	public void print() 
		System.out.println("当前操作系统是windows!");
	


package com.spring4all.convert;

public class LinuxEncodingConvert  implements EncodingConvert

	@Override
	public void print() 
		System.out.println("当前操作系统是Linux!");		
	


4.具体条件实现类

package com.spring4all.convert;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
 * windows条件下
 * @author Bowin
 *
 */
public class WindowsCondition  implements Condition

	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) 
		return context.getEnvironment().getProperty("os.name").contains("Windows");
	


package com.spring4all.convert;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class LinuxCondition  implements Condition

	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) 
		return context.getEnvironment().getProperty("os.name").contains("Linux");  
	


4.测试

package com.spring4all;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.spring4all.convert.EncodingConvert;
/**
 * 启动测试
 * @author Bowin
 *
 */
@SpringBootApplication
public class Application

    public static void main(String[] args) 
        ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
        EncodingConvert bean = run.getBean(EncodingConvert.class);
        bean.print();
        
    









以上是关于Spring Boot @Conditional相关注解解析的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot @Conditional相关注解解析

Spring Boot框架中的Conditional系列注解

Spring Boot框架中的@Conditional系列注解

详解Spring Boot框架中的@Conditional系列注解

详解Spring Boot框架中的@Conditional系列注解

spring boot 系列之六:@Conditional和spring boot的自动配置