Spring boot uses starter to solve many configuration problems, but how did it solve these problems?
Here we use a simple example to see how starter sets the default configuration.
1. Build a starter project
Custom starter, project naming specification is: Custom name-spring-boot-starter
Let's take a look at my last directory structure
1. Modify the pom.xml file
<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> <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>
Actually, it just added spring-boot-autoconfigure
I commented out the main method in the App file, this is not used here
2. Configure the receiving file corresponding to the attributes
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. External Service
package org.elvin;/** * author: Elvin * Date: 2017/12/12 14:55 * Description: */public class HelloService { private String msg; public String saysHello(){ return "Hello " + msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}4. The external service is associated with the configuration corresponding file
package org.elvin;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * author: Elvin * Date: 2017/12/12 14:59 * Description: */@Configuration@EnableConfigurationProperties(HelloServiceProperties.class)@ConditionalOnClass(HelloService.class)@ConditionalOnProperty(prefix = "hello", value="enabled", matchIfMissing =true )public class HelloServiceAutoConfiguration { @Autowired private HelloServiceProperties helloServiceProperties; @Bean @ConditionalOnMissingBean(HelloService.class) public HelloService helloService(){ HelloService helloService = new HelloService(); helloService.setMsg(helloServiceProperties.getMsg()); return helloService; }}5. Starter configuration: spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.elvin.HelloServiceAutoConfiguration
After doing this, use mvn clean install to package it into the maven library
2. Use of spring boot project
Create a new spring boot project and select web.
Directory structure:
Let's take a look at the reference pom.xml
<dependency> <groupId>org.elvin</groupId> <artifactId>my-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
Take a look at HelloController
package org.elvin.learn.springboot.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import 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(); }}The HelloService here is in the previous custom starter.
1. Result: If not configured, it should be output hello world
2. In the configuration file, add hello.msg=hahahahahah
This example is very simple, just to show the main process, and the rest are the logical judgments of each plug-in.
References:
Spring Boot, the disruptor of JavaEE development, practice battle
The above tutorial on implementing spring boot custom starter is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.