スターターハンズオンチュートリアルのカスタムSpringBootシリーズ
Springbootは、スターターの多くを提供している、スターター翻訳シーンがスターターとして理解することができ、ランチャー自動設定を装備した、いわゆるシーン、およびプロジェクトの対応するサービスモジュールので、そのようRabbitMQのを使用する必要のようになります直接プロジェクトを導入する必要がある場合には、春・ブート・スタータのActiveMQに直接導入のいずれか、詳細が参照できますSpringboot公式文書はスターターについて説明します
公式ドキュメントを確認し、以下の命名規則に記載されています。
どの手段が、スプリング・ブートstarter- *として定義される公式スターター、ひどくthirdpartyprojectスプリング・ブート・スターターという名前のカスタムまたはサードパーティの名前SpringBoot
- 公式の名前空間
- 接頭辞: "春-ブートstarter-"
- モード:春-ブートstarter-モジュール名
- 举例:スプリングブートスタータウェブ、ばねブートスタータアクチュエータ、ばねブートスタータJDBC
- カスタム名前空間
- サフィックス:「 - 春 - ブートスターター」
- モード:-springブート・スターターモジュール
- 例:MyBatisのスプリング・ブートスターター
[OK]を、SpringBootは、一例として、ブログの記述を使用するために、いくつかのスターターをカスタマイズし、プロジェクトの開発を容易にするために使用することができますすることができます:
過度の依存のSpringBootせずに新しいプロジェクトを作成し、春・ブート・スターターを導入しなければならない、他の人はJUnitのを除去することができることができ、アプリケーションの種類、およびので、新しい空のプロジェクトを作成することをお勧めして、対応する構成を作成するために、エンジニアリングの方法に導入されていません
pom.xml参照:
<?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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springboot</groupId>
<artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>custom-spring-boot-starter-autoconfigurer</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>
他のスターター、新しいコンフィギュレーション・クラスのプロパティを模倣
package com.example.springboot.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "custom.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;
}
}
テストクラスを書くビジネス:
package com.example.springboot.starter.service;
import com.example.springboot.starter.HelloProperties;
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();
}
}
自動的に設定したカスタムクラスに記述します。
package com.example.springboot.starter;
import com.example.springboot.starter.service.HelloService;
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;
/**
* <pre>
* 自定义的自动配置类
* </pre>
*
* <pre>
* @author nicky.ma
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/01/02 14:31 修改内容:
* </pre>
*/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
新META-INF / spring.factoriesを作成し、次の設定、自動設定クラスは、特定の理由から私の以前の研究ノートを参照することができますソースにスキャンすることができます追加する必要があります。Springbootソースが列を学びます
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.springboot.starter.HelloServiceAutoConfiguration
例では、スターターが作成された、比較的簡単であり、その後、Mavenの設定、カスタムスプリング・ブート・スターター・autoconfigurer依存の導入として、実際にWebプロジェクトを作成する必要があります
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example.springboot</groupId>
<artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
新しいテストを作成するために、インタフェース:
@Autowired
HelloService helloService;
@GetMapping(value = {"/sayHello/{name}"})
@ResponseBody
public String sayHello(@PathVariable("name")String name){
return helloService.sayHello(name);
}
プロジェクトを実行した後、リターンリンクします。http:// localhost:8082 /ウェブ/のsayHello /テスト名
ダウンロードサンプルコード:GitHubのダウンロードリンク