找回密码
 立即注册
查看: 35|回复: 0

9,SpringBoot3注册条件

[复制链接]

260

主题

6

精华

264

金币

技术维护QQ:515138

积分
569
发表于 2026-2-24 17:24:25 | 显示全部楼层 |阅读模式
SpringBoot提供了设置注册生效条件的注解 @Conditional
  
注解
  
  
说明
  
@ConditionalOnProperty
配置文件中存在对应的属性,才声明该bean
@ConditionalOnMissingBean当不存在当前类型的bean时,才声明该bean
@ConditionalOnClass
当前环境存在指定的这个类时,才声明该bean
如果配置文件中配置了指定的信息,则注入,否则不注入
  1. package com.jinhei.config;
  2. import cn.itcast.pojo.Country;
  3. import cn.itcast.pojo.Province;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperties;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. public class CommonConfig {
  11.         // 如果配置文件中配置了指定的信息,则注入,否则不注入
  12.         @ConditionalOnProperty(prefix = "country",name = {"name","system"})
  13.         @Bean //将方法返回值交给Ioc容器管理,成为Ioc容器的bean对象
  14.         public Country country(@Value("${country.name}") String name,@Value("${country.system}") String system){
  15.             Country country = new Country();
  16.             country.setName(name);
  17.             country.setSystem(system);
  18.             return country;
  19.         }
  20.         @Bean// 对象默认名字为方法名
  21.         //如果方法的内部需要使用到IOC容器中已经存在的Bean对象,那么只需要在方法上声明即可,spring会自动的注入
  22.         public Province province(){
  23.             return new Province();
  24.         }
  25. }
复制代码

如果IOC容器中不存在Country对象,则创建Province对象,并注入到IOC容器中,否则不注入
  1. package com.jinhei.config;
  2. import cn.itcast.pojo.Country;
  3. import cn.itcast.pojo.Province;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperties;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. @Configuration
  11. public class CommonConfig {
  12.         // 如果配置文件中配置了指定的信息,则注入,否则不注入
  13.         @ConditionalOnProperty(prefix = "country",name = {"name","system"})
  14.         @Bean //将方法返回值交给Ioc容器管理,成为Ioc容器的bean对象
  15.         public Country country(@Value("${country.name}") String name,@Value("${country.system}") String system){
  16.             Country country = new Country();
  17.             country.setName(name);
  18.             country.setSystem(system);
  19.             return country;
  20.         }
  21.         // 如果IOC容器中不存在Country对象,则创建Province对象,并注入到IOC容器中,否则不注入
  22.         @ConditionalOnMissingBean(Country.class)
  23.         @Bean
  24.         public Province province(){
  25.             return new Province();
  26.         }
  27. }
复制代码

//如果当前环境中存在DispatcherServlet类,则注入Province,否则不注入
// 如果当前引入了web起步依赖,则环境中有DispatcherServlet,否则没有
有以下依赖则注入:
  1. <dependency>
  2.             <groupId>org.springframework.boot</groupId>
  3.             <artifactId>spring-boot-starter-web</artifactId>
  4.         </dependency>
复制代码
3.png
demo4.zip (29.32 KB, 下载次数: 0, 售价: 50 金币)


上一篇:8,Bean管理之Bean注册
下一篇:10,自动配置原理
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2026-3-5 16:46

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表