前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot自定义启动器

springboot自定义启动器

作者头像
叔牙
发布2020-11-19 17:39:39
9150
发布2020-11-19 17:39:39
举报

springboot自定义启动器

介绍

Spring Boot为大多数开源项目提供了许多启动器,很可能你想为你的项目或组织开发自己的自动配置,我们也可以用Spring Boot创建自定义启动器。

在开始之前,我们来讨论一下Spring Boot自动配置是如何工作的。

1

SpringBoot自动配置

1.1:查找自动配置类

在启动我们的应用程序时,Spring Boot会检查名为spring.factories的特定文件,该文件位于META-INF目录中。

这是spring-boot-autoconfigure的一个条目:

# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\

所有自动配置类都应列在spring.factories属性文件中的EnableAutoConfiguration关键字下。

我们重点关注自动配置文件条目中的几个关键点:

  • 根据配置文件,Spring Boot将尝试运行所有这些配置
  • 实际的类配置加载将取决于类路径上的类(例如,如果Spring在类路径中找到JPA,它将加载JPA配置类)

1.2:条件注解

Spring Boot使用注解来确定是否需要配置给定的自动配置类。

@ConditionalOnClass和@ConditionalOnMissingClass注解帮助Spring Boot确定是否需要包含给定的自动配置类,以类似的方式使用@ConditionalOnBean和@ConditionalOnMissingBean进行spring bean级别的自动配置。

以下是MailSenderAutoConfiguration类的示例:

@Configuration @ConditionalOnClass({ MimeMessage.class, MimeType.class }) @ConditionalOnMissingBean(MailSender.class) @Conditional(MailSenderCondition.class) @EnableConfigurationProperties(MailProperties.class) @Import(JndiSessionConfiguration.class) public class MailSenderAutoConfiguration { // required configurations }

Spring Boot还在bean初始化时使用一些默认值,这些默认值基于Spring环境属性。

使用MailProperties类声明@EnableConfigurationProperties,这是此类的代码片段:

@ConfigurationProperties(prefix = "spring.mail") public class MailProperties { private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private Integer port; }

在初始化bean时,MailProperties文件中定义的属性是MailSenderAutoConfiguration类的默认属性,Spring Boot允许我们使用application.properties文件覆盖这些配置属性。

要覆盖默认端口,我们需要在application.properties文件中添加以下条目:

spring.mail.port=445 . (prefix+property name)

2

SpringBoot自定义启动器

要创建我们自己的自定义启动器,我们需要以下组件:

  • 具有自动配置类的autoconfigure模块
  • 启动器模块将使用pom.xml带来所有必需的依赖项

对于这篇文章,我们只创建单个模块,结合自动配置代码和启动器模块,以获取所有必需的依赖项。

我们将创建一个具有以下功能的简单hello服务启动器:

  1. hello-service-spring-boot-starter拥有HelloService,它将名称作为输入来表示问候语。
  2. HelloService将使用默认配置作为默认名称
  3. 创建Spring Boot示例程序以使用我们的hello-service-starter

2.1:自动配置模块

hello-service-spring-boot-starter将具有以下类和配置:

  • HelloServiveProperties文件默认名称
  • HelloService接口和HelloServiceImpl类
  • HelloServiceAutoConfiguration用于创建HelloService实例
  • 用于为我们的自定义启动器提供所需依赖项的pom.xml文件

2.2:属性和服务类

public interface HelloService { void hello(); } //Impl Service public class HelloServiceImpl implements HelloService { @Override public void hello() { System.out.println("Hello from the default starter"); } }

2.3:自动配置模块和类

@Configuration @ConditionalOnClass(HelloService.class) public class HelloServiceAutoConfiguration { //conditional bean creation @Bean @ConditionalOnMissingBean public HelloService helloService(){ return new HelloServiceImpl(); } }

自动配置的最后一部分是在位于/src/main/resources/META-INF中的spring.factories属性文件中添加此类:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.typhoon.config.HelloServiceAutoConfiguration

在应用程序启动时候会执行以下操作:

  • 如果类路径中有HelloService类,则HelloServiceAutoConfiguration将运行(@ConditionOnClass注解)
  • 如果HelloService Bean不可用,则将由Spring Boot创建HelloService Bean(@ConditionalOnMissingBean)
  • 如果开发人员定义了自己的HelloService bean,那么我们的客户启动器将不会创建HelloService Bean

2.4:pom.xml文件

自定义启动器的最后一部分是pom.xml,用于引入所有必需的依赖项:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

让我们来介绍一下pom.xml中的有趣的点:

  • 将其父级定义为spring-boot-starters,这将根据需要引入所需的依赖关系

2.5:命名习惯

在使用Spring Boot创建自定义启动器时,请阅读以下命名约定准则:

  • 自定义启动器模块不应该以Spring Boot启动
  • 使用name-spring-boot-starter作为参照,在我们的例子中,将启动器命名为hello-service-spring-boot-starter

3

使用自定义启动器

让我们创建一个示例Spring Boot应用程序来使用我们的自定义启动器,创建应用程序后,将自定义启动器添加为pom.xml中的依赖项:

<dependency> <groupId>com.typhoon</groupId> <artifactId>hello-service-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>

这是我们的Spring Boot启动类:

@SpringBootApplication public class CustomStarterAppApplication implements CommandLineRunner { @Autowired HelloService service; public static void main(String[] args) { SpringApplication.run(CustomStarterAppApplication.class, args); } @Override public void run(String... strings) throws Exception { service.hello(); } }

如果我们运行我们的应用程序,您将在控制台中看到以下输出:

2018-11-23 20:27:52.138 INFO 20441 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation..... Hello from the default starter 2018-11-23 20:27:52.620 INFO 20441 --- [ main] c.j.CustomStarterAppApplication : Started CustomStarterAppApplication in ....

我们还没有定义任何HelloService是我们的演示应用程序。 当Spring Boot启动时,自动配置没有找到任何自定义bean定义,自定义启动器自动配置类创建了默认的“HelloService”bean(从输出中可见)。

要了解Spring Boot自动配置逻辑和功能,我们在示例应用程序中创建自定义HelloService bean:

public class CustomHelloService implements HelloService { @Override public void hello() { System.out.println("We are overriding our custom Hello Service"); } } //bean bean definition @SpringBootApplication public class CustomStarterAppApplication implements CommandLineRunner { @Autowired HelloService service; public static void main(String[] args) { SpringApplication.run(CustomStarterAppApplication.class, args); } @Override public void run(String... strings) throws Exception { service.hello(); } @Bean public HelloService helloService(){ return new CustomHelloService(); } }

以下是运行此应用程序的输出:

2018-11-23 20:36:48.991 INFO 20529 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup We are overriding our custom Hello Service 2018-11-23 20:36:49.000 INFO 20529 --- [ main] c.j.CustomStarterAppApplication : Started CustomStarterAppApplication in 0.701 seconds

当我们定义自定义bean时,Spring Boot默认的HelloService不再可用。

这使开发人员可以通过创建/提供自己的bean定义来完全覆盖默认bean定义。

总结

在这篇文章中,我们使用Spring Boot创建了自定义启动器,在应用程序中学习了如何使用这些自定义启动器,同时介绍了Spring Boot自动配置如何与启动器配合使用。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-12-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 PersistentCoder 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档