springbootでは、ほとんどがスターターで使用しています。スターターは、あなたが春・ブート・スターター-JDBCを使用することができ、たとえば、あなたはプラグインのJDBCを使用する場合は、プラグイン可能なスタイルのプラグインとして理解することができます。家族のスターター版の上にも増加しています。伝統的なMavenプロジェクトでは通常、いくつかの層のコンポーネントは、モジュールを管理するために、相互に依存するので、多重化は、春ブーツプロジェクトで私たちは、この目的を達成するためのカスタム春ブーツスターターを作成することができます分割します。
A、スターター作品
1、springbootジャーパッケージプロジェクトのスキャンを開始jarファイル検索spring.factoriesを含むパッケージを見つけるために、依存します
図2に示すように、コンフィギュレーション記載spring.factoriesクラスローディングAutoConfiure
3、条件に応じ@Conditional注釈、自動構成および射出バネコンテキストビーン
第二に、カスタムスターター
1、IDEA空のプロジェクトを作成します
図2は、2 Module1を、自動的に設定されている(Mavenプロジェクト)を追加、自動構成を依存しているイニシエータスターター(springboot工学)、です。
3、プロジェクト構造
図4に示すように、内部コード
(1)ばねブートスタータautoconfigurerモジュール
package com.springboot.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author:素小暖
* @date:2020/2/9
* @desc:
*/
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAuto {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
package com.springboot.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author:素小暖
* @date:2020/2/9
* @desc:
*/
@ConfigurationProperties(prefix = "springboot.hello")
public class HelloProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.springboot.starter;
/**
* @author:素小暖
* @date:2020/2/9
* @desc:
*/
public class HelloService {
HelloProperties helloProperties;
public String hello() {
return "hello" + helloProperties.getName();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
spring.factoriesファイル:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.springboot.starter.HelloAuto
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springboot</groupId>
<artifactId>spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
(2)springbootstarter。
スターターは比較的単純である、唯一の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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wms</groupId>
<artifactId>springboot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.springboot</groupId>
<artifactId>spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
5、テスト、新しいspringbootプロジェクト、POMファイルは、ランチャー導入します
<dependency>
<groupId>springboot</groupId>
<artifactId>springboot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
application.propertiesは、設定項目を追加します
springboot.hello.name=chenzz
テストコントローラ。
package com.springboot.teststarter;
import com.wms.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:素小暖
* @date:2020/2/9
* @desc:
*/
@RestController
public class controller {
@Autowired
HelloService helloService;
@GetMapping("hello")
public String hello(){
return helloService.hello();
}
}
5、ブラウザアクセス