説明
ランチャーモジュールは空のjarファイルであり、補助的な依存関係管理のみを提供します。これらの依存関係は、自動アセンブリまたは他のクラスライブラリに使用できます。
名前の削減:
正式名称:
-
プレフィックス:spring-boot-starter-xxx
-
例:spring-boot-starter-web .. ..
カスタムネーミング:
-
xxx-spring-boot-starter
-
例:mybatis-spring-boot-starter
ランチャーを書く
1.IDEAで新しい空のプロジェクトspring-boot-starter-diyを作成します
2.新しい通常のMavenモジュールを作成します:kuang-spring-boot-starter
3.新しいSpringbootモジュールを作成します:kuang-spring-boot-starter-autoconfigure
4.スターターにautoconfigureの依存関係をインポートします!
<!-- 启动器 -->
<dependencies>
<!-- 引入自动配置模块 -->
<dependency>
<groupId>com.kuang</groupId>
<artifactId>kuang-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
5.自動構成プロジェクトの下にあるすべての冗長ファイルを削除します。Pomにはスターターが1つだけ残っています。これは、すべてのスターターの基本構成です。
6.独自のサービスを作成する
package com.kuang;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix() + name + helloProperties.getSuffix();
}
}
7、HelloProperties構成クラスを記述します
package com.kuang;
import org.springframework.boot.context.properties.ConfigurationProperties;
// 前缀 kuang.hello
@ConfigurationProperties(prefix = "kuang.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
8.自動構成クラスを作成し、Beanを注入して、テストします。
package com.kuang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication //web应用生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
9.リソースに独自のMETA-INF \ spring.factoriesを記述します
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kuang.HelloServiceAutoConfiguration
10.書き込みが完了したら、Mavenウェアハウスにインストールできます。
テスト
1.新しいSpringBootプロジェクトを作成します
2.独自のランチャーをインポートします
<dependency>
<groupId>com.kuang</groupId>
<artifactId>kuang-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
3. HelloControllerを作成して、独自に作成したインターフェイスをテストします。
package com.kuang.controller;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String hello(){
return helloService.sayHello("zxc");
}
}
4.構成ファイルapplication.propertiesを記述します
kuang.hello.prefix="ppp"
kuang.hello.suffix="sss"
5.テストのためにプロジェクトを開始すると、結果は成功です。