1.有时候需要根据不同条件生成不同的 bean,这时候就需要用到 @Conditional 注解。常见的一种用法,就是根据操作系统类型,注册不同的 bean。在 BeanConfig 配置类中增加三个 bean。
package io.mieux.config;
import io.mieux.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
@Bean(value = "zhangsan")
public Person getPerson() {
return new Person("zhangsan", 28);
}
@Bean(value = "linux")
public Person getLinux() {
return new Person("linux", 50);
}
@Bean(value = "windows")
public Person getWindows() {
return new Person("windows", 30);
}
@Bean(value = "mac")
public Person getMac() {
return new Person("mac", 24);
}
}
查看当前容器中 Person 类的实例有哪些:
package io.mieux;
import io.mieux.bean.Person;
import io.mieux.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App04 {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
String[] beanNames = annotationConfigApplicationContext.getBeanNamesForType(Person.class);
for (String beanName : beanNames) {
System.out.println("beanName: " + beanName);
}
}
}
运行效果:
beanName: zhangsan
beanName: linux
beanName: windows
beanName: mac
2.使用 @Conditional 注解来根据操作系统的不同注册不同的 bean。首先需要实现 Condition 接口,创建三个 Condition 接口的实现类 LinuxConditon、MacConditon 和 WindowsConditon,获取运行环境的操作系统类型进行判断。
LinuxConditon:
package io.mieux.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
// 判断是否是 linux 系统
public class LinuxConditon implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String operatingSystem = environment.getProperty("os.name");
if ("linux".equals(operatingSystem)) {
System.out.println("Operating System: " + environment.getProperty("os.name"));
return true;
}
return false;
}
}
WindowsConditon:
package io.mieux.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
// 判断是否是 windows 系统
public class WindowsConditon implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String operatingSystem = environment.getProperty("os.name");
if (operatingSystem.contains("Windows")) {
System.out.println("Operating System: " + environment.getProperty("os.name"));
return true;
}
return false;
}
}
MacConditon:
package io.mieux.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
// 判断是否是 mac os 系统
public class MacConditon implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String operatingSystem = environment.getProperty("os.name");
if ("Mac OS X".equals(operatingSystem)) {
System.out.println("Operating System: " + environment.getProperty("os.name"));
return true;
}
return false;
}
}
在 BeanConfig 配置类中加上 @Conditional 注解。
package io.mieux.config;
import io.mieux.bean.Person;
import io.mieux.condition.LinuxConditon;
import io.mieux.condition.MacConditon;
import io.mieux.condition.WindowsConditon;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
@Bean(value = "zhangsan")
public Person getPerson() {
return new Person("zhangsan", 28);
}
@Bean(value = "linux")
@Conditional(value = {LinuxConditon.class})
public Person getLinux() {
return new Person("linux", 50);
}
@Bean(value = "windows")
@Conditional(value = {WindowsConditon.class})
public Person getWindows() {
return new Person("windows", 30);
}
@Bean(value = "mac")
@Conditional(value = {MacConditon.class})
public Person getMac() {
return new Person("mac", 24);
}
}
运行效果:
Operating System: Mac OS X
beanName: zhangsan
beanName: mac
网友评论