A Spring Boot usa o iniciador para resolver muitos problemas de configuração, mas como resolveu esses problemas?
Aqui usamos um exemplo simples para ver como o iniciador define a configuração padrão.
1. Construa um projeto inicial
O iniciante personalizado, a especificação de nomeação do projeto é: personalizado nomes-spring-boot-starter
Vamos dar uma olhada na minha última estrutura de diretório
1. Modifique o arquivo 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.apache.org/POM/4.0.0 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-autoconfigure</artifactId> <version>1.5.9.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <PuerpId> org.apache.maven.plugins </frupidId> <stifactId> maven-compiler-plugin </stutifactId> <versão> 2.3.2 </versão </configuration> </fonte> 1.8 </fonte> <-Target> 1.8 </Target> <//Configuration> </plugin> </plugins> </plukins> </builtins> </sOrber> </Target> 1.8 <///////, </figuration> </plugin> </plugins> </plugins> </butins> </sOroc>
Na verdade, ele apenas adicionou primavera-boot-autoconfigure
Eu comentei o método principal no arquivo de aplicativos, isso não é usado aqui
2. Configure o arquivo de recebimento correspondente aos atributos
pacote org.elvin; importar org.springframework.boot.context.properties.configurationproperties;/** * autor: elvin * data: 2017/12/12 14:51 * descrição: */@configationProperties (prefix = "hello") public classe helloservicePerts { String privada msg = msg; public String getMsg () {return msg; } public void setmsg (string msg) {this.msg = msg; }}3. Serviço externo
pacote org.elvin;/** * Autor: Elvin * Data: 2017/12/12 14:55 * Descrição: */classe pública Heloservice {private string msg; public string dizhello () {return "hello" + msg; } public string getMsg () {return msg; } public void setmsg (string msg) {this.msg = msg; }}4. O serviço externo está associado à configuração correspondente do arquivo
pacote org.elvin; importar org.springframework.beans.factory.annotation.aUtowired; importar org.springframework.boot.autoconfigure.condition.conditionalonClass; importação de org.springframe.ingiss.Atoconfigure.ConditionOnMalMonmingMOIROLOTOLOT.ATOCONFIGURE.CONDIÇÃO org.springframework.boot.autoconfigure.condition.conditionalOnProperty; importar org.springframework.boot.context.properties.enableConfigurationProperties; importação; * Autor: Elvin * Data: 2017/12/12 14:59 * Descrição: */@Configuration@EnableConfigurationProperties (HelloServiceProperties.class) @conditionOnClass (Heloservice.class) @ConditionalOnProperty (prefix = "hellowerD =" habilitado "habilitado), @ConditionalOnProperty (prefix =" hello ", value =" {@AUTowired Private HeloserviceProperties HeloserviceProperties; @Bean @conditionalonMissingBean (Helloservice.class) Helloservice Helloservice () {Helloservice Helloservice = new Helloservice (); helloservice.setmsg (helloserviceProperties.getmsg ()); retornar Helloservice; }}5. Configuração inicial: Spring.Factories
org.springframework.boot.autoconfigure.enableautoconfiguration = org.elvin.helloserviceAutoconfiguration
Depois de fazer isso, use o MVN Clean Install para empacotá -lo na Biblioteca Maven
2. Uso do projeto de inicialização da primavera
Crie um novo projeto de inicialização da primavera e selecione Web.
Estrutura do diretório:
Vamos dar uma olhada na referência pom.xml
<Depencency> <GrupidId> org.elvin </frugiD> <stifactId> my-spring-boot-starter </artifactId> <versão> 1.0-snapshot
Dê uma olhada no Hellocontroller
pacote org.elvin.learn.springboot.controller; importar org.springframework.beans.factory.annotation.autowired; importar org.springframework.web.bind.annotation.requestmapping; importação.sPringFramework.web.bind.annotation.requestmapping; importação.sPringFramework.web.bind.annotation.RequestMapping; importação.sPringFramework.web.bind.annotation.RequestMapping; importar.sPringFramework.web.bind.annotation.requestmapping; importar.sPringFramework.web.bind.anotatória org.springframework.web.bind.annotation.restcontroller; importar org.elvin. *;/** * Autor: Elvin * Data: 2017/12/12 15:34 * Descrição: */@RestController@requestmapping ("Hello) Hellocontroller; @RequestMapping ("Index") public String Index () {return helloservice.sayhello (); }}O Heloservice aqui está no iniciante personalizado anterior.
1. Resultado: se não estiver configurado, deve ser a saída do Hello World
2. No arquivo de configuração, adicione hello.msg = hahahahahahh
Este exemplo é muito simples, apenas para mostrar o processo principal, e o restante são os julgamentos lógicos de cada plug-in.
Referências:
Boot de primavera, o disruptor do desenvolvimento de Javaee, Batalha de Prática
O tutorial acima sobre a implementação do Spring Boot Custom Starter é todo o conteúdo que compartilhei com você. Espero que você possa lhe dar uma referência e espero que você possa apoiar mais o wulin.com.