0


SpringBoot的@ConditionalOnMissingBean注解的作用

1、@ConditionalOnMissingBean

@ConditionalOnMissingBean,它是修饰bean的一个注解,主要实现的是,当你的bean被注册之后,如果而注册相同类型的bean,就不会成功,它会保证你的bean只有一个,即你的实例只有一个。

如果不加@ConditionalOnMissingBean,当你注册多个相同的bean时,会出现异常,以此来告诉开发人员。

package ljxwtl.cn.bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: wtl
 * @License: (C) Copyright 2021, wtl Corporation Limited.
 * @Contact: [email protected]
 * @Date: 2021/12/29 10:29 上午
 * @Version: 1.0
 * @Description:
 */
@Configuration
public class UserConfigure {

    @Bean
    public MyTestInjection createBean1(){
        return new MyTestInjection();
    }

    @Bean
    public MyTestInjection createBean2(){
        return new MyTestInjection();
    }
}

这样就会报错!!!

报错信息如下:

当我们把 @ConditionalOnMissingBean放到一个bean注入时,则就不会报错:

package ljxwtl.cn.bean;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: wtl
 * @License: (C) Copyright 2021, wtl Corporation Limited.
 * @Contact: [email protected]
 * @Date: 2021/12/29 10:29 上午
 * @Version: 1.0
 * @Description:
 */
@Configuration
public class UserConfigure {

    @Bean
    public MyTestInjection createBean1(){
        return new MyTestInjection();
    }

    @Bean
    @ConditionalOnMissingBean
    public MyTestInjection createBean2(){
        return new MyTestInjection();
    }
}

当我们把

 @ConditionalOnMissingBean

去掉之后,你的bean可以注册多次,这时需要用的**@Primary**来确定你要哪个实现;一般来说,对于自定义的配置类,我们应该加上@ConditionalOnMissingBean注解,以避免多个配置同时注入的风险。

2、@Primary

package ljxwtl.cn.bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author: wtl
 * @License: (C) Copyright 2021, wtl Corporation Limited.
 * @Contact: [email protected]
 * @Version: 1.0
 * @Description:
 */
@Configuration
public class UserConfigure {

    @Bean
    public MyTestInjection createBean1(){
        return new MyTestInjection();
    }

    @Bean
    @Primary
    public MyTestInjection createBean2(){
        return new MyTestInjection();
    }
}

3、@ConditionalOnProperty

通过其三个属性prefix,name以及havingValue来实现的,其中prefix表示配置文件里节点前缀,name用来从application.properties中读取某个属性值,havingValue表示目标值。

  • 如果该值为空,则返回false;
  • 如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。
  • 返回值为false,则该configuration不生效;为true则生效。

下面代码演示为配置文件lind.redis.enable为true时才会注册RedisFactory这个bean:

package ljxwtl.cn.bean;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author: wtl
 * @License: (C) Copyright 2021, wtl Corporation Limited.
 * @Contact: [email protected]
 * @Version: 1.0
 * @Description:
 */
@Configuration
@ConditionalOnProperty(prefix="lind.redis",name = "enable", havingValue = "true")
public class UserConfigure {

    @Bean
    public MyTestInjection createBean1(){
        return new MyTestInjection();
    }
}

然后再application.properties里配置:

这样就成功了!!!

4、@ConditionalOnBean

@ConditionalOnBean // 当给定的在bean存在时,则实例化当前Bean

@ConditionalOnMissingBean // 当给定的在bean不存在时,则实例化当前Bean 
@ConditionalOnClass // 当给定的类名在类路径上存在,则实例化当前Bean 

@ConditionalOnMissingClass // 当给定的类名在类路径上不存在,则实例化当前Bean
需求场景

比如下面一种场景,我在实例化People对象的时候,需要注入一个City对象。这个时候问题来了,如果city没有实例化,那么下面就会报空指针或者直接报错。
所以这里需求很简单,就是当前city存在则实例化people,如果不存在则不实例化people,这个时候

@ConditionalOnBean

的作用来了。

package ljxwtl.cn.bean;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author: wtl
 * @License: (C) Copyright 2021, wtl Corporation Limited.
 * @Contact: [email protected]
 * @Version: 1.0
 * @Description:
 */
@Configuration
public class UserConfigure {

    @Bean
    @ConditionalOnBean(name = "city")
    public People createPeople(City city){
        System.out.println(city.getCityName());
        return new People();
    }

    @Bean
    public City createCity(){
        City city = new City();
        city.setCityName("北京");
        return city;
    }
}

**在

spring ioc

的过程中,优先解析

@Component,@Service,@Controller

注解的类。其次解析配置类,也就是

@Configuration

标注的类。最后开始解析配置类中定义的

bean

。**

**示例代码中

people

是定义在配置类中的,当执行到配置类解析的时候,

@Component,@Service,@Controller ,@Configuration

标注的类已经全部被解析,所以这些

BeanDifinition

已经被同步。**

**但是

bean1

的条件注解依赖的是

city

city

是被定义的配置类中的,因为两个

Bean

都是配置类中

Bean

,所以此时配置类的解析无法保证先后顺序,就会出现不生效的情况。**

**同样的道理,如果依赖的是

FeignClient

,也有可能会出现不生效的情况。因为

FeignClient

最终还是由配置类触发,解析的先后顺序也不能保证。**

解决

有两种方式:

  • 项目中条件注解依赖的类,大多会交给spring容器管理,所以如果要在配置中Bean通过@ConditionalOnBean依赖配置中的Bean时,完全可以用@ConditionalOnClass(Bean2.class)来代替。
  • 如果一定要区分两个配置类的先后顺序,可以将这两个类交与EnableAutoConfiguration管理和触发。也就是定义在META-INF\spring.factories中声明是配置类,然后通过@AutoConfigureBefore、AutoConfigureAfter、AutoConfigureOrder控制先后顺序。因为这三个注解只对自动配置类生效。

本文转载自: https://blog.csdn.net/wtl1992/article/details/122210599
版权归原作者 狂飙-高启强-张颂文-王天龙 所有, 如有侵权,请联系我们删除。

“SpringBoot的@ConditionalOnMissingBean注解的作用”的评论:

还没有评论