Create a component com-itpsc-service. The component has only one service.

Pom file:

< the groupId > com. Itpsc < / groupId > < artifactId > com - itpsc - service < / artifactId > < version > 1.0 - the SNAPSHOT < / version > <packaging>jar</packaging> <name>com-itpsc-service</name> <description>com-itpsc-service</description>Copy the code

Write UserService class

public class UserService {

private String username;

private String password;

public void print() {
 System.out.println("username="+username + " password="+password);
}

public String getUsername() {
  return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
 return password;
}

public void setPassword(String password) {
this.password = password;
}
Copy the code

}

MVN install package package into the Maven repository.

Create a starter Create a startup component named ITPSc-spring-boot-starter

The spring-boot-starter, spring-boot-autoconfigure, and spring-boot-configuration-processor are introduced

These jars rely on the ability to write automatic configuration classes, annotations, generate configuration metadata processing, and so on.

<groupId>com.itpsc.spring.boot</groupId> <artifactId>itpsc-spring-boot-starter</artifactId> The < version > 1.0 - the SNAPSHOT < / version > < packaging > jar < / packaging > < dependencies > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> < version > 2.0.4. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework. Boot < / groupId > < artifactId > spring - the boot - autoconfigure < / artifactId > < version > 2.0.4. RELEASE < / version > < / dependency > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> < version > 2.0.4. RELEASE < / version > < / dependency > < the dependency > < groupId > com. Itpsc < / groupId > <artifactId>com.itpsc.service</artifactId> </dependency> </dependenciesCopy the code

Write the automatic configuration class UserProperties.java to convert the configuration of the specified prefix in the configuration file (YML/Properties) to beans using the @ConfigurationProperties annotation.

package com.itpsc.spring.boot.starter; . @ConfigurationProperties(prefix = "com.itpsc") public class UserProperties { private String username; private String password; . }Copy the code

UserAutoConfiguration. Java, @ Configuration comments make class bean plant.

@ EnableConfigurationProperties annotations make @ ConfigurationProperties annotations to take effect.

package com.itpsc.spring.boot.starter; . @Configuration @EnableConfigurationProperties(UserProperties.class) public class UserAutoConfiguration { @Bean public UserService getBean(UserProperties UserProperties) {// Create component instance UserService UserService = new UserService(); userService.setUsername(userProperties.getUsername()); userService.setPassword(userProperties.getPassword()); return userService; }}Copy the code

The spring.factories file \ meta-inf \spring.factories is used to define the classes that need to be configured automatically. Springboot will instantiate the objects. The configuration file is loaded by loading the SpringFactoriesLoader class, which loads the configuration classes in the file into the Spring container.

Create a new meta-INF folder in SRC /main/resources and a new spring.factories file in the meta-INF folder. The configuration is as follows:

Org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration = com. Itpsc. Spring. The boot. The starter. UserAutoConfiguration back to the top The starter idea terminal uses the MVN install package command to package the package into the Maven repository.

Testing the starter

Starter is introduced in the Springboot – Mybatis – Demo project

<dependency> <groupId>com.itpsc.spring.boot</groupId> <artifactId>itpsc-spring-boot-starter</artifactId> < version > 1.0 - the SNAPSHOT < / version > < / dependency >Copy the code

When you configure username and password in YML, you can see the automatic prompt. This is because the imported JAR contains metadata files, as detailed below.

com: itpsc: username: “itpsc” password: itpsc@123 image

Metadata files are automatically generated by the compiler by processing all nodes annotated by @ConfigurationProperties.

wps418F.tmp[4]

Testing is adding a test method

@Autowired private UserService userService; @Test public void testItpscStarter() { userService.print(); } the results: the 2019-01-23 20:22:41. 17184-615 the INFO [main]. I. pringbootMybatisDemoApplicationTests: Started SpringbootMybatisDemoApplicationTests in 11.505 seconds (JVM running for 14.582) username = itpsc password=itpsc@123Copy the code

It can be seen from the running result that the bean of the JAR package in the starter package that we encapsulated has been automatically configured, indicating that our starter package is successful. Here are a few more details about metadata mentioned above.

Back to top Metadata The SpringBoot JAR contains metadata files that provide details of all supported configuration properties. These files are designed to allow IDE developers to provide context assistance and auto-completion when users use application.properties or application.yml files.

The main metadata files are generated automatically at the compiler by processing all nodes annotated by @ConfigurationProperties.

The configuration metadata is located in the META-INF/spring-configuration-metadata.json jar file using a simple JSON format with “groups” or “properties” categorical nodes.

wps89DA.tmp

{ “sourceType”: “org.springframework.boot.autoconfigure.web.ServerProperties”, “defaultValue”: 8080, “name”: “server.port”, “description”: “Server HTTP port.”, “type”: “java.lang.Integer” }, { “defaultValue”: “/”, “deprecated”: true, “name”: “server.servlet-path”, “description”: “Path of the main dispatcher servlet.”, “type”: “java.lang.String”, “deprecation”: { “level”: “error”, “replacement”: “server.servlet.path” }

The corresponding json nodes server.port and server.servlet-path can be defined in yML or properties files

Server: port: 8081 Context-path: / If you do not know whether spring supports a configuration, you can check the metadata file to see if the corresponding node exists.