Spring Boot utilise le démarreur pour résoudre de nombreux problèmes de configuration, mais comment a-t-il résolu ces problèmes?
Ici, nous utilisons un exemple simple pour voir comment Starter définit la configuration par défaut.
1. Construisez un projet de démarrage
Démarreur personnalisé, Spécification de dénomination du projet est: Nom-Spring-Spring-Starter personnalisé
Jetons un coup d'œil à ma dernière structure de répertoire
1. Modifier le fichier pom.xml
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <ModelVersion> 4.0.0 </ ModelVersion> <ProupId> org.elvin </rompuprid> <ArtifActid> My-Spring-Boot-Starter </Retifactid> <version> 1.0-SNAPSHOT </DERNEMENT> <name> my-spring-boot-starter </name> <url> http://maven.apache.org </url> <projeties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </properframes> <dépendants> <dependency> <prouprid> org.springworkwoot <pouprences>> <proupdid> org.springworkwoot <pouprences>> <proupdid> org.springworkwork <poupled>> <prouprid> org.springworkwork <boupred>> <prouprid> org.springworkwork <poupled>> <prouprid> org.springworkwork <boupred>> <ArtefactId> Spring-Boot-AutoConfigure </ Artifactid> <Dersion> 1.5.9.Release </DERNIERSE> </ Dependency> <Dependency> <ProupID> JUnit </proncID> <Artefactid> JUnit </ Artifactid>> 3.8.1 </ Version> <Scope> Test </cope> </Dependency> </Detencescy> <Duild> <Duild> Test> Test> <proupId> org.apache.maven.plugins </rompuprid> <letifactive> maven-compiler-plugin </ artifactive> <version> 2.3.2 </-version> <fonfi configuration> <source> 1.8 </source> </ible> 1.8 </ Target> </figuration> </Kingin> </glugins> </uild> </prètement>
En fait, il vient d'ajouter le spring-boot-autoconfigure
J'ai commenté la méthode principale du fichier de l'application, ce n'est pas utilisé ici
2. Configurez le fichier de réception correspondant aux attributs
Package org.elvin; import org.springframework.boot.context.properties.configurationproperties; / ** * Auteur: Elvin * Date: 2017/12/12 14:51 * Description: * / @ ConfigurationProperties (Prefix = "Hello") Classe publique MSG = "World"; chaîne privée msg = msg; String public getmsg () {return msg; } public void setmsg (String msg) {this.msg = msg; }}3. Service externe
Package org.elvin; / ** * Auteur: Elvin * Date: 2017/12/12 14:55 * Description: * / public class Helloservice {private String msg; String public Sayshello () {return "Hello" + msg; } public String getmsg () {return msg; } public void setmsg (String msg) {this.msg = msg; }}4. Le service externe est associé au fichier correspondant de configuration
package org.elvin; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.autoconfigure.condition.conditionalonclass; import org.springframework.boot.autoconfigure.condition.conditionalonMistingBan; org.springframework.boot.autoconfigure.condition.conditionalonproperty; import org.springframework.boot.context.properties.enableConfigurationProperties; import org.springframework.context.annotation.bean; Import Org.springFrameworkworkworkwork.Contex Elvin * Date: 2017/12/12 14:59 * Description: * / @ configuration @ perteConfigurationProperties (HellOserviceProperties.class) @ConditionalOnClass (Helloservice.class) @conditionalonProperty) @Autowired Private HelloserviceProperties HelloserviceProperties; @Bean @conditionalonMissingBean (HellOservice.class) public Helloservice Helloservice () {Helloservice Helloservice = new HellOservice (); HelloService.setmsg (HelloServiceProperties.getmsg ()); Retour Helloservice; }}5. Configuration du démarreur: Spring.factories
org.springframework.boot.autoconfigure.enableAutoConfiguration = org.elvin.helloserviceAutoconfiguration
Après cela, utilisez l'installation MVN Clean pour l'emballer dans la bibliothèque Maven
2. Utilisation du projet Spring Boot
Créez un nouveau projet Spring Boot et sélectionnez Web.
Structure du répertoire:
Jetons un coup d'œil à la référence pom.xml
<dependency> <proupId> org.elvin </proupId> <ErtifactId> my-spring-boot-starter </ artifactid> <version> 1.0-snapshot </-version> </dependency>
Jetez un œil à HelloController
package org.elvin.learn.springboot.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.anquestMapping; org.springframework.web.bind.annotation.restController; Import org.elvin. *; / ** * Auteur: Elvin * Date: 2017/12/12 15:34 * Description: * / @ @AutController @ requestmapping ("Hello") public classwoShip; @RequestMapping ("index") public String index () {return helloService.sayHello (); }}Le Helloservice ici est dans le démarreur personnalisé précédent.
1. Résultat: s'il n'est pas configuré, il doit être sorti Hello World
2. Dans le fichier de configuration, ajoutez Hello.msg = hahahahahah
Cet exemple est très simple, juste pour montrer le processus principal, et les autres sont les jugements logiques de chaque plug-in.
Références:
Spring Boot, le perturbateur du développement de Javaee, pratiquez la bataille
Le tutoriel ci-dessus sur l'implémentation de Spring Boot Custom Starter est tout le contenu que j'ai partagé avec vous. J'espère que vous pourrez vous faire référence et j'espère que vous pourrez soutenir Wulin.com plus.