一、前置知識:
maven項目打包參考連接配接
二、自定義starter
1.命名規範
自定義的Starter官方建議:叫xxx-spring-boot-starter。
2.建立maven項目,結構如下
3.添加pom的依賴
紫色為重點
<?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>
<groupId>com.fast</groupId>
<artifactId>fast-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fast-spring-boot-starter</name>
<description>fast-spring-boot-starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.6.7</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.16</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
</dependencies>
</project>
4.編寫自己的核心代碼Myservice.java
package com.fast;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(MyService.class)
//注意:prefix的值不能用駝峰命名,springboot2.x不識别,例:myService新項目引入導緻無法啟動,正例:myservice或my-service
@ConfigurationProperties(prefix = "myservice")
public class MyService {
private int id;
private String name;
//構造方法,set,get,toString省略
}
5.編寫自動配置類MyServiceAutoConfiguration.java
package com.fast;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyServiceAutoConfiguration {
static {
System.out.println("加載MyServiceAutoConfiguration");
}
@Bean
//注意紫色部分叫什麼,使用@Autowaired注入的時候就叫什麼
public MyService myService() {
return new MyService();
}
}
6.在Resource下建立META-INF目錄,再在新目錄下建立spring.factories
紫色部分為固定寫法,後面跟自己的自動配置類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.fast.MyServiceAutoConfiguration
7.install或上傳到私服,在新項目的pom中引用即可。
測試:
1.在新項目的application.yml中添加myservice
myservice:
id: 11
name: 李四
2.在測試類中編寫測試
注意:包結構和src/java下的包結構要一緻,否則會報錯。
package com.fast;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SysJobTest {
@Autowired
private MyService myService;
@Test
public void startTest() throws Exception {
System.out.println("myService = " + myService);
}
}
三、熱插拔
如果不使用熱插拔,坐标引入即意味着功能開啟,如果使用了熱插拔,則使用條件觸發,例如:在啟動類上添加指定注解才允許開啟。
1.把上面的工程接着改造,結構如下
2.新增标記類ConfigMarker.java
package com.fast;
/**
* 标記類
*/
public class ConfigMarker {
}
3.新增注解開關EnableRegisterServer
package com.fast;
import org.springframework.context.annotation.Import;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
//@Import這個注解是熱插拔的核心,這個注解會将ConfigMapper生成執行個體,并注入到容器中
@Import({ConfigMarker.class})
public @interface EnableRegisterServer {
}
4.改造自動配置類
注意紫色部分,改造好之後,重新install或上傳到私服
package com.fast;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//檢測到容器中有ConfigMarker執行個體存在,就會自動加載下面的代碼,反之,不加載
@ConditionalOnBean(ConfigMarker.class)
public class MyServiceAutoConfiguration {
static {
System.out.println("加載MyServiceAutoConfiguration");
}
@Bean
public MyService myService() {
return new MyService();
}
}
5.在新項目中的啟動類上添加開關注解
package com.fast;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.DependsOn;
@SpringBootApplication
//注掉這個注解,則項目啟動時不會加載fast-spring-boot-starter的myService,是以容器中沒有這個對象;
//放開這個注解,則項目啟動時會在ioc容器中注入myService執行個體,做到了自動加載自定義starter
@EnableRegisterServer
@MapperScan("com.fast.dao")
public class FastBootApplication {
public static void main(String[] args) {
SpringApplication.run(FastBootApplication.class, args);
}
}