@Conditional
- 作用范围
类和方法上
作用在类上 主要加在@Component或者@Configuration 组件上面
- <li>as a type-level annotation on any class directly or indirectly annotated with
- {@code @Component}, including {@link Configuration @Configuration} classes</li>
- <li>as a meta-annotation, for the purpose of composing custom stereotype
作用在方法 用于@Bean 的method上面
- <li>as a method-level annotation on any {@link Bean @Bean} method</li>
- 属性
Class 是实现了Condition 接口的类
/**
- All {@link Condition Conditions} that must {@linkplain Condition#matches match}
- in order for the component to be registered.
*/
Class<? extends Condition>[] value();
/**
* Indicates that a component is only eligible for registration when all
* {@linkplain #value specified conditions} match.
*
* <p>A <em>condition</em> is any state that can be determined programmatically
* before the bean definition is due to be registered (see {@link Condition} for details).
*
* <p>The {@code @Conditional} annotation may be used in any of the following ways:
* <ul>
* <li>as a type-level annotation on any class directly or indirectly annotated with
* {@code @Component}, including {@link Configuration @Configuration} classes</li>
* <li>as a meta-annotation, for the purpose of composing custom stereotype
* annotations</li>
* <li>as a method-level annotation on any {@link Bean @Bean} method</li>
* </ul>
*
* <p>If a {@code @Configuration} class is marked with {@code @Conditional},
* all of the {@code @Bean} methods, {@link Import @Import} annotations, and
* {@link ComponentScan @ComponentScan} annotations associated with that
* class will be subject to the conditions.
*
* <p><strong>NOTE</strong>: Inheritance of {@code @Conditional} annotations
* is not supported; any conditions from superclasses or from overridden
* methods will not be considered. In order to enforce these semantics,
* {@code @Conditional} itself is not declared as
* {@link java.lang.annotation.Inherited @Inherited}; furthermore, any
* custom <em>composed annotation</em> that is meta-annotated with
* {@code @Conditional} must not be declared as {@code @Inherited}.
*
* @author Phillip Webb
* @author Sam Brannen
* @since 4.0
* @see Condition
*/
Condition
- @FunctionalInterface
- ConditionContext
ConditionContext 可以获取以下类
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
ClassLoader classLoader = context.getClassLoader();
Environment environment = context.getEnvironment();
BeanDefinitionRegistry registry = context.getRegistry();
ResourceLoader resourceLoader = context.getResourceLoader();
- AnnotatedTypeMetadata 获取注解上的信息
@FunctionalInterface
public interface Condition {
/**
* Determine if the condition matches.
* @param context the condition context
* @param metadata the metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
* or {@link org.springframework.core.type.MethodMetadata method} being checked
* @return {@code true} if the condition matches and the component can be registered,
* or {@code false} to veto the annotated component's registration
*/
boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}
ConditionDemo
public class ConditionDemo {
@Conditional(LinuxEnvironment.class)
@Bean(value = "lilei")
public Person person(){
return new Person("李磊");
}
@Conditional(WindowsEnvironment.class)
@Bean(value = "xiaohong")
public Person person2(){
return new Person("小红");
}
public static void main(String[] args) {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(ConditionDemo.class);
String[] beanDefinitionNames = app.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
Person bean = app.getBean(Person.class);
System.out.println(bean);
}
}
class Person{
String name;
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
class LinuxEnvironment implements Condition{
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return Utils.isMathOsName(context.getEnvironment(),"linux");
}
}
class WindowsEnvironment implements Condition{
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return Utils.isMathOsName(context.getEnvironment(),"Windows 10");
}
}
class Utils{
public static boolean isMathOsName(Environment environment,String osName) {
String os_name = environment.getProperty("os.name");
if(os_name.contains(osName)){
return true;
}
return false;
}
}
实现Condition的另一种方式
使用配置文件配置
[图片上传失败...(image-c7331f-1648970105031)]