首页 > 其他分享 >Springboot自定义Stater

Springboot自定义Stater

时间:2022-10-09 12:01:29浏览次数:40  
标签:启动器 Springboot 自定义 spring boot public Stater hello starter


1、默认启动器

Boot会将项目中常用的场景做成对应的starter启动器,项目中涉及到什么场景就引入该场景对应的启动器,项目中引入这些启动器之后,和这个starter相关的依赖也会被引入。比如引入测试和基本的启动器

Springboot自定义Stater_后端

 再如这里引入web开发的启动器

Springboot自定义Stater_xml_02

其他stater查看官方文档

Springboot自定义Stater_xml_03

  

Springboot自定义Stater_spring boot_04

 

2、自定义启动器

如上,boot已经提供了好多常用的starter场景,即使这样有时候我们需要将自己一个公用的业务模块抽成一个starter,需要用到的地方直接使用starter即可,比如分析

分析官网starter定义方式,如引入测试starter

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

分析可以看出如下,starter没有任何实现,只是依赖管理,底层的依赖是一个自动配置类和其他依赖

Springboot自定义Stater_spring_05

底层依赖有个spring-boot-test-autoconfigure依赖用于和boot配置

Springboot自定义Stater_java_06

 

按照上面的模式,定义一个starter步骤如下

  • 启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库。
  • 需要专门写一个类似spring-boot-autoconfigure的配置模块
  • 用的时候只需要引入启动器starter,就可以使用自动配置了

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

我们定义hello-spring-boot-starter,创建一个starter,是一个空的jar项目
 

 

Springboot自定义Stater_后端_07

再创建一个自动配置项目hello-spring-boot-starter-autoconfig

Springboot自定义Stater_xml_08

 

依赖 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>hello-spring-boot-starter-autoconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-spring-boot-starter-autoconfig</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>


</project>

自动配置类

/**
* 当前类是一个自动配置类
* 只有当容器中没有hello组件的时候才会注入
* 和配置文件绑定
*/
@Configuration
@ConditionalOnMissingBean({Hello.class})
@EnableConfigurationProperties({HelloProperties.class})
public class HelloAutoConfiguration {
@Bean
public Hello hello() {
Hello hello = new Hello();
return hello;
}
}

和自动配置类绑定的配置文件类

@ConfigurationProperties("hello")
public class HelloProperties {
private String pre;
private String end;

public String getPre() {
return pre;
}

public void setPre(String pre) {
this.pre = pre;
}

public String getEnd() {
return end;
}

public void setEnd(String end) {
this.end = end;
}
}

主业务类

/**
* 默认不需要自动放入容器中
*/
public class Hello {

@Autowired
HelloProperties helloProperties;

public String sysHello(String userName){
return helloProperties.getPre()+userName+helloProperties.getEnd();
}
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.auto.HelloAutoConfiguration

最后将这两个项目install,然后再其他工程引入starter即可

<dependency>
  <groupId>org.example</groupId>
  <artifactId>hello-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

且可以配置文件中配置

hello.pre=zhangsan
hello.end=你好
 

标签:启动器,Springboot,自定义,spring,boot,public,Stater,hello,starter
From: https://blog.51cto.com/u_11334685/5740088

相关文章