Spring Boot verwendet Starter, um viele Konfigurationsprobleme zu lösen, aber wie hat es diese Probleme gelöst?
Hier verwenden wir ein einfaches Beispiel, um zu sehen, wie Starter die Standardkonfiguration festlegt.
1. Erstellen Sie ein Starterprojekt
Benutzerdefinierte Starter, Projektnamenspezifikation laut
Schauen wir uns meine letzte Verzeichnisstruktur an
1. Ändern Sie die Datei pom.xml
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.elvin</groupId> <artifactId>my-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>my-spring-boot-starter</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-Boot-Autokonfigur </artifactId> <version> 1.9.release </Version> </abhängig> <abhängigkeit> <gruppe> junit </Groupid> <artifactID> junit </artifactId> <version> 3.8.1 </Version> <scope> test </scope> </cheaps> </§/Scope> </teufl <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build></project>
Eigentlich fügte es gerade Spring-Boot-Autokonfigur hinzu
Ich habe die Hauptmethode in der App -Datei kommentiert. Dies wird hier nicht verwendet
2. Konfigurieren Sie die Empfangsdatei, die den Attributen entspricht
Paket org.elvin; import org.springframework.boot.context.properties.configurationProperties;/** * Autor: Elvin * Datum: 2017/12/12 14:51 * Beschreibung: */@configurationProperties (Prefix = "Hallo". private String msg = msg; public String getmsg () {return msg; } public void setmsg (String msg) {this.msg = msg; }}3.. Externer Service
Paket org.elvin;/** * Autor: Elvin * Datum: 2017/12/12 14:55 * Beschreibung: */öffentliche Klasse HelloService {private String msg; public String sayshello () {return "hello" + msg; } public String getmsg () {return msg; } public void setmsg (String msg) {this.msg = msg; }}4.. Der externe Dienst ist der Konfigurationendatei zugeordnet
Paket org.elvin; import org.springframework.bean.factory.annotation org.springframework.boot.autoconfigure.condition.conditionalonProperty; import org.springframework.boot.context.properties.enableConfigurationProperties; importieren org.springFramework.Anntation.Bean; * Autor: Elvin * Datum: 2017/12/12 14:59 * Beschreibung: */@configuration@EnableConfigurationProperties (HelloServiceProperties.ClASS) @ConditionalonClass (HelloService.Class) @ConditionalonProperty (Präfix). @Autowired Private HelloServiceProperties HelloServiceProperties; @Bean @Conditionalonmissingbean (HelloService.Class) public HelloService HelloService () {HelloService HelloService = new HelloService (); HelloService.setmsg (HelloServiceProperties.getMsg ()); HelloService zurückkehren; }}5. Starterkonfiguration: Spring.Factores
org.springframework.boot.autoconfigure.enableAutoconfiguration = org.elvin.helloServiceAutoConfiguration
Verwenden Sie danach die MVN Clean Installation, um es in die Maven -Bibliothek zu verpacken
2. Verwendung des Spring Boot -Projekts
Erstellen Sie ein neues Spring Boot -Projekt und wählen Sie Web.
Verzeichnisstruktur:
Schauen wir uns die Referenz pom.xml an
<Depopenty> <gruppe> org.elvin </Groupid> <artifactId> my-ring-boot-starter </artifactId> <version> 1.0-snapshot </Version> </abhängig>
Schauen Sie sich Hellocontroller an
paket org.elvin.learn.springboot.controller; import org.springframework.bean.factory.Annotation.autowired; org.springframework.web.bind.annotation.restcontroller; import org.elvin. @RequestMapping ("index") public String index () {return helloService.sayhello (); }}Der HelloService hier ist im vorherigen benutzerdefinierten Starter.
1. Ergebnis: Wenn nicht konfiguriert, sollte es Hello World ausgeben
2. Fügen Sie in der Konfigurationsdatei Hallo hinzu.msg = hahahahahahah
Dieses Beispiel ist sehr einfach, nur um den Hauptprozess zu zeigen, und der Rest sind die logischen Urteile jedes Plug-Ins.
Referenzen:
Spring Boot, der Disruptor von Javaee Development, Übungschlacht
Das obige Tutorial zur Implementierung von Spring Boot Custom Starter ist der gesamte Inhalt, den ich mit Ihnen geteilt habe. Ich hoffe, Sie können Ihnen eine Referenz geben und ich hoffe, Sie können wulin.com mehr unterstützen.