首页手机spring配置优先级 springboot优先加载bean

spring配置优先级 springboot优先加载bean

圆圆2025-09-06 21:00:32次浏览条评论

spring boot 条件化 bean 加载详解

本文旨在探讨研究 Spring Boot 中如何使用 @ConditionalOnProperty 注解实现 Bean 的条件化加载。我们将通过一个实际示例详细,讲解如何配置和使用该注解,以实现根据配置属性动态地加载不同的 Bean。通过本文的学习,你将掌握根据不同环境或配置缓存加载 Bean 的方法,从而提高 Spring Boot 应用的灵活性和可维护性。 @ConditionalOnProperty 实现 Bean 的条件化加载

在 Spring Boot 应用中,我们经常需要根据不同的配置来加载不同的 Bean。方便,在开发环境和生产环境中使用不同的数据源,或者根据用户的配置启用或禁用某些功能。Spring Boot 提供了 @ConditionalOnProperty 注解,可以直接实现 Bean 的条件化加载。

@ConditionalOnProperty注解允许我们根据指定的属性值来决定是否创建Bean。该注解包含以下几个重要的属性:prefix: 属性的总线,用于指定配置属性的命名空间。name:属性的名称,用于指定需要检查的配置属性。havingValue:属性的值,只有当配置属性的值与havingValue相同时,才会创建Bean。matchIfMissing:如果默认配置属性不,存在匹配。为false,表示不匹配。示例:根据配置选择加载不同的组件

我们假设有一个应用,需要根据配置选择加载ComponentAConfig或ComponentBConfig。

首先,我们定义一个通用的 ComponentConfigPart 类,用于表示组件的配置部分:@Data@NoArgsConstructorpublic class ComponentConfigPart { private String ex1; private String ex2;定义 private String ex3;}登录后复制

然后,我们一个抽象的 ComponentConfig 类,作为 ComponentAConfig 和 ComponentBConfig 的基类:@Datapublic abstract class ComponentConfig { private Listlt;ComponentConfigPartgt; parts = new ArrayListlt;gt;(); @PostConstruct public void init() { System.out.println(quot;已创建 quot 实例; this.getClass().getSimpleName()); System.out.println(quot;已创建 quot; this); }}登录后复制

接下来,我们定义 ComponentAConfig 和 ComponentBConfig 类,并使用 @ConditionalOnProperty注解来指定加载条件:@Component@ConfigurationProperties(prefix = quot;application.components.aquot;)@ConditionalOnProperty(prefix = quot;applicationquo​​t;, name = quot;usequot;,havingValue = quot;componentAquot;)@ToString(callSuper = true)public class ComponentAConfig extends ComponentConfig {}登录后复制@Component@ConfigurationProperties(prefix = quot;application.components.bquot;)@ConditionalOnProperty(prefix = quot;applicationquo​​t;, name = quot;usequot;,havingValue = quot;componentBquot;)@ToString(callSuper = true)public class ComponentBConfig extends ComponentConfig {}登录复制后

在上面的代码中,ComponentAConfig 只有当 application.use 属性的值为 componentA 时才会被加载,而 ComponentBConfig 只有当 application.use 属性的值为 componentB 时才会被加载。

星火作家大神

星火作家大神是一篇面向作家的AI写作工具 31 查看详情

最后,我们定义了一个 MainConfig 类,用于注入选择的组件:@Configurationpublic class MainConfig { @Autowired private ComponentConfig config; @PostConstruct public void init() { System.out.println(quot;MainConfig has autowired class of quot; config.getClass().getSimpleName()); }}登录后复制配置属性

为了让结果正常工作,我们需要在 application.properties 或 application.yml 文件中配置相应的属性。以下是一个 application.yml 文件的示例:application: Components: a: parts: - ex1: a ex2: aa ex3: aaa - ex1: a2 ex2: aa2 ex3: aaa2 b: parts: - ex1: b ex2: bb ex3: bbb - ex1: b2 ex2: bb2 ex3: bbb2 用途: componentA登录后复制

在该配置中,application.use属性的值为componentA,因此只有ComponentAConfig会被加载。

运行结果

当 application.use 属性的值为 componentA 时,控制台会输出以下信息:Created instance of ComponentAConfigCreated ComponentAConfig(super=ComponentConfig(parts=[ComponentConfigPart(ex1=a, ex2=aa, ex3=aaa), ComponentConfigPart(ex1=a2, ex2=aa2, ex3=aaa2)]))MainConfig has autowired class of ComponentAConfig登录后复制

当 application.use 属性的值为 componentB 时,控制台会输出以下信息:Created instance of ComponentBConfigCreated ComponentBConfig(super=ComponentConfig(parts=[ComponentConfigPart(ex1=b, ex2=bb, ex3=bbb), ComponentConfigPart(ex1=b2, ex2=bb2, ex3=bbb2)]))MainConfig has autowired class of ComponentBConfig登录后复制注意事项@ConditionalOnProperty 注解只能用于配置类或组件类上。@ConditionalOnProperty注解可以与其他条件化注解(如@ConditionalOnClass、@ConditionalOnBean等)一起使用,以实现更复杂的条件化加载逻辑。确保配置属性的名称和值与@ConditionalOnProperty注解中的配置一致。总结

通过论文的学习,我们了解了如何使用@ConditionalOnProperty注解实现Spring Boot @ConditionalOnProperty注解提供了一种简单而强大的方式,可以根据配置属性动态地加载不同的Bean,从而Spring Boot应用的灵活性和提高可维护性。在实际开发中,我们可以根据具体的需求,灵活地使用@ConditionalOnProperty注解,以实现各种复杂的调节加载场景。

以上就是Spring Boot调节Bean加载详解的详细内容,更多请关注乐哥常识网其他相关文章! 相关标签: app ai red spring spring boot 命名空间

Spring Boo
React项目Webpack配置现代化与性能优化指南 react webcomponent
相关内容
发表评论

游客 回复需填写必要信息