1 介绍
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
/**
* All {@link Condition Conditions} that must {@linkplain Condition#matches match}
* in order for the component to be registered.
*/
Class<? extends Condition>[] value();
}
作用
根据条件注入bean对象
属性
用于提供一个Condition接口的实现类,实现类中需要编写具体的代码实现注入条件
Condition接口介绍
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.core.type.AnnotatedTypeMetadata;
@FunctionalInterface
public interface Condition {
boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}
package study.wyy.spring.anno.contional.spi;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Map;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/22 7:40 下午
*
*/
public class WindowsConditional implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 1 获取bean工厂
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
// 2 获取环境信息
Environment environment = context.getEnvironment();
// 输出一下所有的环境信息
System.out.println("输出一下所有的环境信息。。。。。");
StandardEnvironment standardEnvironment = (StandardEnvironment) environment;
Map<String, Object> systemEnvironment = standardEnvironment.getSystemProperties();
for(Map.Entry<String,Object> entry: systemEnvironment.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("输出一下所有的环境信息。。。。");
// 3 获取bean定义信息的注册器
BeanDefinitionRegistry registry = context.getRegistry();
// 4 获取ResourceLoader
ResourceLoader resourceLoader = context.getResourceLoader();
return false;
}
}
2 演示
如下配置类
package study.wyy.spring.anno.contional.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import study.wyy.spring.anno.common.model.Person;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/22 7:23 下午
*/
@Configuration
public class SpringConfig {
@Bean
public Person windowsAdmin(){
return new Person("windowsAdmin",18);
}
@Bean
public Person macAdmin(){
return new Person("macAdmin",18);
}
@Bean
public Person linuxAdmin(){
return new Person("linuxAdmin",18);
}
}
如何做到根据系统运行的环境注入对应环境的Person对象呢
要想实现这个功能就可以通过@Conditional
注解
实现Condition接口
- window环境实现类
package study.wyy.spring.anno.contional.spi;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/22 7:40 下午
*/
public class WindowsConditional implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 1 获取环境信息
Environment environment = context.getEnvironment();
String os = environment.getProperty("os.name");
if(os.contains("Windows")){
return true;
}
return false;
}
}
- mac环境实现类
package study.wyy.spring.anno.contional.spi;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/22 7:40 下午
*/
public class MacConditional implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 1 获取环境信息
Environment environment = context.getEnvironment();
String os = environment.getProperty("os.name");
if(os.contains("Mac")){
return true;
}
return false;
}
}
- linux环境实现类
package study.wyy.spring.anno.contional.spi;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/22 7:40 下午
*/
public class LinuxConditional implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 1 获取环境信息
Environment environment = context.getEnvironment();
String os = environment.getProperty("os.name");
if(os.contains("Linux")){
return true;
}
return false;
}
}
使用Conditional注解
package study.wyy.spring.anno.contional.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import study.wyy.spring.anno.common.model.Person;
import study.wyy.spring.anno.contional.spi.LinuxConditional;
import study.wyy.spring.anno.contional.spi.MacConditional;
import study.wyy.spring.anno.contional.spi.WindowsConditional;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/22 7:23 下午
*/
@Configuration
public class SpringConfig {
@Bean("windowsAdmin")
@Conditional(value = WindowsConditional.class)
public Person windowsAdmin(){
return new Person("windowsAdmin",18);
}
@Bean("macAdmin")
@Conditional(value = MacConditional.class)
public Person macAdmin(){
return new Person("macAdmin",18);
}
@Bean("linuxAdmin")
@Conditional(value = LinuxConditional.class)
public Person linuxAdmin(){
return new Person("linuxAdmin",18);
}
}
测试
package study.wyy.spring.anno.contional.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import study.wyy.spring.anno.common.model.Person;
import study.wyy.spring.anno.contional.config.SpringConfig;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/22 7:25 下午
*/
public class Test {
@org.junit.Test
public void test01(){
AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext(SpringConfig.class);
Person admin = context.getBean(Person.class);
System.out.println(admin);
}
}
输出: mac系统下测试
Person(name=macAdmin, age=18)