Spring Boot 自动配置之条件注解浅析

下面我将为您详细讲解“Spring Boot 自动配置之条件注解浅析”的完整攻略,包含两条示例说明。

1. Spring Boot自动配置原理

Spring Boot的自动配置原理是通过条件注解来实现的。Spring Boot启动时,会默认扫描项目中所有的@Configuration注解,然后根据条件注解(@ConditionalOnXxx)来判断该配置是否需要生效。

例如,一个@Configuration注解的类中如果有@ConditionalOnClass注解,则表示只有当项目中存在指定的类时,该@Configuration才会生效。@ConditionalOnClass注解的实现如下:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
    /**
     * The classes that must be present. Since this annotation is parsed by loading class
    * bytecode, it is safe to specify classes here that may ultimately not be on the classpath,
    * although doing so will generally not be useful unless the intention is to test for the
    * presence of a class and fail fast when it is not available.
    * @return the classes that must be present
    */
    Class<?>[] value() default {};
}

2. 示例一:条件注解@ConditionalOnWebApplication

条件注解@ConditionalOnWebApplication表示只有当项目是一个Web应用时,该@Configuration才会生效。@ConditionalOnWebApplication注解的实现如下:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnWebApplication {
    /**
    * The type of web application to check for.
    * @return the required type of web application
    */
    WebApplicationType type() default WebApplicationType.SERVLET;
}

我们可以通过以下步骤来演示它的使用:

步骤一:创建一个Spring Boot的Maven项目,命名为“web-demo”;

步骤二:添加如下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

这里我们添加了spring-boot-starter-web依赖,用于启动一个Web应用。

步骤三:创建一个@Configuration注解的类,并在该类上添加@ConditionalOnWebApplication注解。代码如下:

@Configuration
@ConditionalOnWebApplication
public class MyConfig {
    @Bean
    public String myBean() {
        return "Hello, World!";
    }
}

步骤四:创建一个SpringBootApplication,启动应用并访问http://localhost:8080,会看到如下结果:

Hello, World!

这说明我们的@Configuration生效了,因为项目是一个Web应用。

3. 示例二:条件注解@ConditionalOnMissingBean

条件注解@ConditionalOnMissingBean表示只有当指定的Bean不存在时,该@Configuration才会生效。@ConditionalOnMissingBean注解的实现如下:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnMissingBeanCondition.class)
public @interface ConditionalOnMissingBean {
    /**
    * The class of bean that should not be present.
    * @return the class of bean that should not be present
    */
    Class<?>[] value() default {};
}

我们可以通过以下步骤来演示它的使用:

步骤一:创建一个Spring Boot的Maven项目,命名为“bean-demo”;

步骤二:添加如下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

这里我们添加了spring-boot-starter-web依赖,用于启动一个Web应用。

步骤三:创建一个@Service注解的类,命名为MyService。代码如下:

@Service
public class MyService {
    public void run() {
        System.out.println("My Service is running...");
    }
}

步骤四:创建一个@Configuration注解的类,命名为MyConfig,并在该类上添加@ConditionalOnMissingBean注解。代码如下:

@Configuration
public class MyConfig {
    @Bean
    @ConditionalOnMissingBean(MyService.class)
    public String myBean() {
        return "Hello, World!";
    }
}

步骤五:创建一个SpringBootApplication,启动应用并访问http://localhost:8080,会看到如下结果:

Hello, World!

步骤六:将MyService改为如下代码:

@Service
public class MyService {
    public void run() {
        System.out.println("My Service is running...");
    }
}

步骤七:重新启动应用并访问http://localhost:8080,会看到如下结果:

My Service is running...

这说明@Configuration不生效了,因为我们的MyService存在了,条件不满足。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring Boot 自动配置之条件注解浅析 - Python技术站

(0)
上一篇 2023年5月16日
下一篇 2023年5月16日

相关文章

  • Go语言获取系统性能数据gopsutil库的操作

    要使用gopsutil库,首先需要在Go代码中安装它,可以使用以下命令: go get github.com/shirou/gopsutil 安装完成后,我们需要导入gopsutil库,以便在代码中使用它。导入命令如下: import ( "github.com/shirou/gopsutil/cpu" "github.com/…

    GitHub 2023年5月16日
    00
  • 如何使用pm2快速将项目部署到远程服务器

    Sure,下面是如何使用pm2快速将项目部署到远程服务器的完整攻略。 什么是pm2? PM2 是一个基于 Node.js 进程管理工具,它具有负载均衡,0 秒停机重载等强大的应用管理功能。 pm2的安装 在开始使用pm2之前,首先需要在远程服务器上安装pm2。你可以使用Node.js包管理器npm进行安装: npm install -g pm2 安装完成之后…

    GitHub 2023年5月16日
    00
  • 解决fatal:remote error:You can’t push to git://github.com/username/*.git问题的办法

    当使用 git 命令将本地代码推送到 Github 远程仓库时,有时可能会遇到以下错误提示信息: fatal: remote error: You can’t push to git://github.com/username/*.git 此错误提示信息通常意味着您正在尝试使用 SSH 克隆 Github 上的一个只读 Git 仓库,或者直接通过 git:/…

    GitHub 2023年5月16日
    00
  • 大数据分析R语言RStudio使用超详细教程

    准备工作 在使用R语言进行数据分析之前,首先需要安装R语言和RStudio。R语言是一种用于统计分析和数据可视化的编程语言,可以在其官网(https://www.r-project.org/)下载最新版本的安装程序。而Rstudio,则是一种集成开发环境,可以相对方便地进行代码编写和管理,可以在其官网(https://rstudio.com/)下载最新版本的…

    GitHub 2023年5月16日
    00
  • 使用Golang玩转Docker API的实践

    本文主要介绍如何使用Golang玩转Docker API,并提供两个示例代码说明。 什么是Docker API Docker API 是一个 RESTful API,它允许应用程序访问Docker守护进程,以创建、修改和删除Docker对象(如容器、映像、网络等)。 如何使用Golang访问Docker API 要使用Golang访问Docker API,需…

    GitHub 2023年5月16日
    00
  • 使用cache加快编译速度的命令详解

    下面我来为你详细讲解“使用cache加快编译速度的命令详解”的完整攻略。 1. 缓存介绍 在计算机领域里,缓存是一种读写速度非常快的存储器,通俗地说,缓存就好像是一张用来存储经常使用的东西的纸条,当需要获取这些东西时,我们可以先查看纸条上的内容,这样就可以快速找到并获取到我们需要的东西了。类似的,缓存也是这样的道理。 在编译一些较大程序时,每次编译都需要重新…

    GitHub 2023年5月16日
    00
  • 详解git的基本使用方法

    详解Git的基本使用方法 什么是Git? Git是一个分布式版本控制系统,常用于代码管理和版本控制。相比其他版本控制系统,Git具有分支管理、本地版本控制和远程服务器交互等方面的优势。Git采用对等的分布式系统,可以让每个开发人员在自己的本地机器上克隆仓库,先在自己的本地环境里正确运行、测试和修改代码,然后再统一提交到公共仓库中,方便其他开发人员查看和合并代…

    GitHub 2023年5月16日
    00
  • 详解Eclipse提交项目到GitHub以及解决代码冲突

    如何将项目提交到Github? 在Eclipse中,可以使用EGit插件来将项目提交到Github。具体步骤如下: 在Eclipse中安装EGit插件。在Eclipse中选择“Help” → “Eclipse Marketplace”,然后搜索“EGit”,选择“Install”,安装完成后重启Eclipse。 在Github上创建一个Repository。…

    GitHub 2023年5月16日
    00
合作推广
合作推广
分享本页
返回顶部