###原理:

是通过Condition接口判断pom.xml有没有导入某个坐标或者依赖某个坐标而进行加载某个bean。

Condition是Spring4.0后引入的条件化配置接口,通过实现Condition接口可以完成有条件的加载相应的Bean,

然后通过@Conditional注解,要配和Condition的实现类(ClassCondition)进行使用

###需求

在spring的ioc容器中有一个User的Bean,如果pom.xml导入了Jedis坐标后,就加载该Bean,否则不加载

###代码实现:

1、先创建一个user对象

public class User {}

2、创建一个UserConfig.java的配置类,生成User对象的Bean

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class UserConfig {
@Bean
public User user(){
return new User();
}}

3、在Springboot的启动类中获取User对象的Bean

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublic class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
//在Springboot的启动类中获取User对象的Bean
Object user = run.getBean("user");
System.out.println(user);//输入:com.example.demo.User@6724cdec
}}

###代码改造

1、创建一个类实现Condition接口,重写matches方法,然后在买手机游戏账号平台方法里面写条件。返回true代表加载user对象的bean,返回false表示不加载

import org.springframework.context.annotation.Condition;import org.springframework.context.annotation.ConditionContext;import org.springframework.core.type.AnnotatedTypeMetadata;public class ClassCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//需求:如果pom.xml导入了Jedis坐标后,就加载该User对象的Bean,否则不加载
boolean flag=true;
try {
//如果导入了Jedis坐标可以通过放射的形式获取到Jedis对象。
Class<?> clazz = Class.forName("redis.clients.jedis.Jedis");

} catch (ClassNotFoundException e) {
//如果程序报错,证明没有导入Jedis坐标,所以获取不到
flag=false;

}
return flag;
}}

2、在UserConfig.java类中创建User对象Bean 的方法上加上@Conditional(ClassCondition.class)注解,注解里面传入要判断的条件实现类,根据该类的matches方法的返回值进行操作,返回true代表可以加载User对象的bean,返回false表示不可以加载

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Conditional;import org.springframework.context.annotation.Configuration;@Configurationpublic class UserConfig {
@Bean
@Conditional(ClassCondition.class) //加入条件判断的实现类,根据返回值判断是否要加载该bean
public User user(){
return new User();
}}

4、测试

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId></dependency>

你在pom.xml导入了上面的坐标,运行springboot的启动类,如果有就正常获取,如果没有程序就会报错,提示找不到user的bean

更多相关文章

  1. 【前端】打开网站时loading是怎么判断资源是否加载完的?
  2. 【前端】在electron使用ckplayer播放rtmp在开发环境可以播放,打包
  3. vue_3
  4. 动态分页制作及composer自动加载及第三方验证码使用
  5. 补0817:域名空间内类实现自动加载,use的作用 及 mysql数据库DDL,D
  6. 08-11 作业 面对对象和自动加载 封装 继承 多态
  7. 命名空间自动加载进阶与mysql初体验
  8. mysql语句、类的自动加载与引用
  9. 总结mysql 常用DDL, DML语言并实操与use在命名空间中的作用及实

随机推荐

  1. android studio ndk编译问题
  2. Android联系人数据库全解析(5)
  3. android layouts之RelativeLayout
  4. react-native APP图标和Splash(Android)
  5. 移植android的工作环境
  6. 2015-10-30-02-Android Theme主题使用心
  7. Android 重力感应 翻转页面造成Activity
  8. android ListView向下展开 抽屉效果
  9. android 关于字符转化问题
  10. Android文档-开发者指南-第一部分:入门-中