首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >spring注解@Conditional 按照一定的条件进行判断,满足条件给容器中注册bean

spring注解@Conditional 按照一定的条件进行判断,满足条件给容器中注册bean

作者头像
全栈程序员站长
发布2022-11-17 10:14:26
发布2022-11-17 10:14:26
4850
举报

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
public class Person {
	
	private String name ;
	
	private int age  ;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public Person() {
		super();
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	

}

@Conditional({}) 按照一定的条件进行判断,满足条件给容器中注册bean * 在类上使用表示,满足条件会执行这个类,如果不满足则类中所有方法都不会加载 * 在方法上使用表示,满足条件会执行这个方法

代码语言:javascript
复制
/**
* @Conditional({}) 按照一定的条件进行判断,满足条件给容器中注册bean
* ** 在类上使用表示,满足条件会执行这个类,如果不满足则类中所有方法都不会加载
* 如果系统是windows,给容器注册("bill")
* 如果系统是linux,给容器注册("linus")
*/
@Conditional({WindowsCondition.class})
@Configuration
public class MainConfig2 {

	@Lazy
	@Bean
	public Person person() {
		System.out.println("创建----------->>>>>>>>>");
		return new Person("李四",99);
	}
	
	/**
	 * @Conditional({}) 按照一定的条件进行判断,满足条件给容器中注册bean
	 * ** 在方法上使用表示,满足条件会执行这个方法
	 * 如果系统是windows,给容器注册("bill")
	 * 如果系统是linux,给容器注册("linus")
	 */
	@Conditional({WindowsCondition.class})
	@Bean("bill")
	public Person person2() {
		return new Person("bill gates",66);
	}
	
	@Conditional({LinuxCondition.class})
	@Bean("linus")
	public Person person3() {
		return new Person("linus",49);
	}
	
}
代码语言:javascript
复制
public class WindowsCondition implements Condition{
	/**
	 * 判断是否是Windows系统
	 * ConditionContext:判断条件能使用的上下文(环境)
	 * AnnotatedTypeMetadata:注释信息
	 */
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

		//1、能获取到ioc使用的beanfactory
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		//2、获取类加载器
		ClassLoader classLoader = context.getClassLoader();
		//3、获取当前环境信息
		Environment environment = context.getEnvironment();
		//4、获取到bean定义的注册类
		BeanDefinitionRegistry registry = context.getRegistry();
		
		String property = environment.getProperty("os.name");
		if(property.contains("Windows")) {
			return true;
		}
		return false;
	}
}
代码语言:javascript
复制
public class LinuxCondition implements Condition{

	/**
	 * 判断是否是linux系统
	 * ConditionContext:判断条件能使用的上下文(环境)
	 * AnnotatedTypeMetadata:注释信息
	 */
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

		//1、能获取到ioc使用的beanfactory
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		//2、获取类加载器
		ClassLoader classLoader = context.getClassLoader();
		//3、获取当前环境信息
		Environment environment = context.getEnvironment();
		//4、获取到bean定义的注册类
		BeanDefinitionRegistry registry = context.getRegistry();
		
		String property = environment.getProperty("os.name");
		if(property.contains("Linux")) {
			return true;
		}
		return false;
	}
}
代码语言:javascript
复制
	@SuppressWarnings("resource")
	@Test
	public void test02() {
		AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(MainConfig2.class);
		System.out.println(Arrays.deepToString(acac.getBeanDefinitionNames()));
		String[] definitionNames = acac.getBeanDefinitionNames();
		for (String string : definitionNames) {
			System.out.println(string);
		}
	}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/209958.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月25日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档