ITPub博客

首页 > IT职业 > IT生活 > SpringBoot中Bean自动装配原理

SpringBoot中Bean自动装配原理

IT生活 作者:jhmaple 时间:2021-09-09 08:51:05 0 删除 编辑

###原理:

是通过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;
@Configuration
public 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;
@SpringBootApplication
public 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
    }
}

以上是不管什么时候都能加载User对象的Bean,但是我们的需求是如果pom.xml导入了Jedis坐标后,就加载该Bean,否则不加载,所以接下来就行修改

###代码改造

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;
@Configuration
public class UserConfig {
    @Bean
    @Conditional(ClassCondition.class) //加入条件判断的实现类,根据返回值判断是否要加载该bean
    public User user(){
        return new User();
    }
}

4、测试

dependency>
    groupId>redis.clientsgroupId>
    artifactId>jedisartifactId>
dependency>

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


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1678/viewspace-2796637/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    547
  • 访问量
    6029