Spring Boot menggunakan Starter untuk menyelesaikan banyak masalah konfigurasi, tetapi bagaimana cara menyelesaikan masalah ini?
Di sini kami menggunakan contoh sederhana untuk melihat bagaimana starter mengatur konfigurasi default.
1. Bangun Proyek Starter
Starter Kustom, Spesifikasi Penamaan Proyek adalah: nama-boot-boot-starter kustom
Mari kita lihat struktur direktori terakhir saya
1. Ubah file pom.xml
<Project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http:/maven.romp.romp.romp. http://maven.apache.org/xsd/maven-4.0.0.xsd "> <podelversion> 4.0.0 </ModelVersion> <groupid> org.elvin </sroupid> <Artifactid> my-stboot-starter </arttifactid> <cackaging> <packaging- packaging> </arttifacaging </artfacagaging </artfacagaging> </artfacaging </artfacaging </artfacaging> </artfacaging </art <name> My-SPRING-BOTT-STARTER </name> <Url> http://maven.apache.org </rerl> <proypent> <proyform.build.sourceencoding> UTF-8 </project.build.sourceencoding> </Properties> <pendendencies> </project.build.sourceencoding> </Properties> <pendendencies> </project. <ArTifactId> Spring-Boot-Autoconfigure </RaintifactId> <version> 1.5.9.release </version> </gandendency> <dependency> <groupid> Junit </groupid> <ArtifactId> Junit </arttifactid> <version> 3.8.1 </Versi> <cope> Test </scopeR> </Scope> </Versies> </Version> <cope> <cope> Test </scope> </Scope> <groupId> org.apache.maven.plugins </groupid> <ArTifactId> maven-compiler-plugin </artifactid> <version> 2.3.2 </version> <configururation> <source> 1.8 </source> <target> 1.8 </target> </configuration> </plugin> </source> <it/1.8 </target> </configuration> </plugin> </Plugins> <//projok
Sebenarnya, itu baru saja menambahkan otokonfigure boot-boot
Saya mengomentari metode utama dalam file aplikasi, ini tidak digunakan di sini
2. Konfigurasikan file penerima yang sesuai dengan atribut
package org.elvin;import org.springframework.boot.context.properties.ConfigurationProperties;/** * author: Elvin * Date: 2017/12/12 14:51 * Description: */@ConfigurationProperties(prefix = "hello")public class HelloServiceProperties { //Default configuration content private static final String MSG = "world"; Private String msg = msg; Public String getMSG () {return msg; } public void setMSG (string msg) {this.msg = msg; }}3. Layanan Eksternal
Paket org.elvin;/** * Penulis: Elvin * Tanggal: 2017/12/12 14:55 * Deskripsi: */kelas publik HelloService {private string msg; public string sayshello () {return "hello" + msg; } public String getMSG () {return msg; } public void setMSG (string msg) {this.msg = msg; }}4. Layanan eksternal dikaitkan dengan file yang sesuai konfigurasi
Paket org.elvin; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.boot.autoconfigure.condition.conditionAnclass; import org.spramework.boot.autoconfigure.condition.condition org.springframework.boot.autoconfigure.condition.conditionAlonproperty; impor org.springframework.boot.context.properties.enableconfigurationProperties; impor org.springframework.context.annotation.bean; org.springframework.context.annotation.configuration;/** * Penulis: Elvin * Tanggal: 2017/12/12 14:59 * Deskripsi: */@configuration@enableconfigurationproperties (helloServiceCclass (classon) @conditionAntonclass (helloService (helloService (helloService) @conditionlasclass (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloService (helloservice) value = "diaktifkan", matchifmissing = true) kelas publik HelloServiceAutoconfiguration {@Autowired Private HelloServiceProPerties HelloServiceProPerties; @Bean @ConditionAnonMissingBean (HelloService.class) Publik HelloService HelloService () {HelloService HelloService = HelloService baru (); HelloService.setMSG (HelloServicePropersies.GetMSG ()); Kembalikan HelloService; }}5. Konfigurasi Starter: Spring.Factories
org.springframework.boot.autoconfigure.enableAutoconfiguration = org.elvin.helloserviceAutoconfiguration
Setelah melakukan ini, gunakan MVN Clean Install untuk mengemasnya ke dalam Maven Library
2. Penggunaan Proyek Boot Musim Semi
Buat Proyek Spring Boot baru dan pilih Web.
Struktur Direktori:
Mari kita lihat referensi pom.xml
<dependency> <GroupId> org.elvin </groupid> <ArTifactId> my-spring-boot-starter </stifactid> <version> 1.0-snapshot </version> </dependency>
Lihatlah HelloController
Paket org.elvin.learn.springboot.controller; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.web.bind.annotation.requestmapping; impor org.springframework.wind.notation.requestmapping; orgconconramework.bind.bind.annotation.requestmapping; org.springframework.web.bind.annotation.RestController;import org.elvin.*;/** * author: Elvin * Date: 2017/12/12 15:34 * Description: */@RestController@RequestMapping("hello")public class HelloController { @Autowired private HelloService helloService; @RequestMapping ("index") public string index () {return helloService.sayhello (); }}HelloService di sini ada di starter kustom sebelumnya.
1. Hasil: Jika tidak dikonfigurasi, itu harus output hello world
2. Di file konfigurasi, tambahkan hello.msg = hahahahah
Contoh ini sangat sederhana, hanya untuk menunjukkan proses utama, dan sisanya adalah penilaian logis dari setiap plug-in.
Referensi:
Spring Boot, Pengganggu Pengembangan Javaee, Latihan Pertempuran
Tutorial di atas tentang menerapkan starter kustom Spring Boot adalah semua konten yang telah saya bagikan dengan Anda. Saya harap Anda dapat memberi Anda referensi dan saya harap Anda dapat mendukung wulin.com lebih lanjut.