When working with the springBoot project, we often use various JARS named ***-starterFor example, mybatis-spring-boot-starter, shiro-spring-boot-web-starter and so on.When we use these services, we marvel at their magic, just a few lines of configuration can enjoy all kinds of convenience, so how to write this service?Today we build one from scratch, to see how it works
The basic composition of starter is a simple SpringMVC project with a series of configurations and annotations to realize the magic of autowerization.
One: Project initialization
1: First we will create a basic Springboot project called my-starter
2: Modify poM files
First let’s remove the springBoot dependency
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 2.1.6. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --></parent>Copy the code
Then add the spring-boot-Dependencies dependency of the same version as the deleted dependencies under the dependencyManagement TAB
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> < version > 2.1.6. RELEASE < / version > <type>pom</type> <scope>import</scope></dependency>Copy the code
Then the POM file after our modification should be:
<? xml version="1.0" encoding="UTF-8"? ><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. The demo < / groupId > < artifactId > my - starter < / artifactId > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>my-starter</name> <description>Demo projectforSpring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > </properties> <dependencies> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> < version > 2.1.6. RELEASE < / version > <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>Copy the code
3: Add dependencies
In our last post on @SpringBootApplication, we mentioned @conditionalonClass as an annotation
To recap what it does: indicates that the specified class must exist or the configuration class that the annotation modifies will not be resolved.
So we use him to realize the function of automatic assembly. He is in the spring-boot-autoconfigure package, so we add dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId></dependency>Copy the code
Two: Add Properties configuration
Create MyStarterProperties. Java
package org.demo.mystarter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "my-starter")
public class MyStarterProperties {
private String name = "chuchen";
private String projectName = "my-starter";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) { this.projectName = projectName; }}Copy the code
The @configurationProperties (prefix = “my-starter”) is used to bind configuration data to the object
We’ll use it in the future.
my-starter.name=***
my-starter.project-name=***Copy the code
We can set the default value or not set the attribute. If the default value is set, it can not be configured when using it. Otherwise, it needs to be configured
Three: Create a business class
Create MyStarterService. Java
package org.demo.mystarter;
public class MyStarterService {
private String name;
private String projectName;
public String getMyStarterMsg() {return "author is " + name + ",project_name is " + projectName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) { this.projectName = projectName; }}Copy the code
Four: automatic assembly
package org.demo.mystarter;
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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MyStarterProperties.class)
@ConditionalOnClass(MyStarterService.class)
public class MyStarterAutoConfiguration {
@Autowired
private MyStarterProperties myStarterProperties;
@Bean
@ConditionalOnMissingBean(MyStarterService.class)
MyStarterService myStarterService() {
MyStarterService myStarterService = new MyStarterService();
myStarterService.setName(myStarterProperties.getName());
myStarterService.setProjectName(myStarterProperties.getProjectName());
returnmyStarterService; }}Copy the code
@ EnableConfigurationProperties and @ ConditionalOnClass article has said
@ ConditionalOnMissingBean role only when the BeanFactory does not contain the specified in the bean class conditions to create a bean
Five: Configuration file
What else is important in @SpringBootApplication besides these files?
Is a spring.factories file in the meta-INF directory
Create meta-INF → spring.Factories in the Resources folder
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
org.demo.mystarter.MyStarterAutoConfigurationCopy the code
Six: Jar into the local Maven repository
Select the project, click Maven on the right, and then select Install under Lifecycle.
If the following figure is displayed, the port is added successfully
Seven: testing
1: Create a SpringBoot project again, importing the dependencies we just packaged into the POM file
<dependency> <groupId>org.demo</groupId> <artifactId>my-starter</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / dependency >Copy the code
2: create the controller
package com.chuchen.teststarter;
import org.demo.mystarter.MyStarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestContrsoller {
@Autowired
private MyStarterService myStarterService;
@GetMapping("/test")
public String test() {returnmyStarterService.getMyStarterMsg(); }}Copy the code
3: the browser to http://localhost:8080/test
4: Modify the configuration file
Open the application. The properties
my-starter.name=beany
my-starter.project-name=testStarterCopy the code
To visit again
Eight: the last
My series on SpringBoot:
SpringBoot series tutorials