Create a custom Starter:
The emergence of Springboot greatly simplifies the configuration of developers, and one of the great tools is the starter of springboot, which is the core component of springboot. The official springboot also encapsulates various conveniences for developers. Starter module used, for example:
- spring-boot-starter-web//spring MVC相关
- spring-boot-starter-aop //Aspect programming related
- spring-boot-starter-cache //Cache related
(1) Customized Starter requirements: The
official website requires the creation of two modules, one is an autoconfigure module and the other is a starter module. The starter module depends on the autoconfigure module, which mainly serves as a transitive dependency (can be omitted).
(2) Naming convention: The
official starter spring-boot-starter-xxx
is named in the format, and the starter customized by third-party developers xxxx-spring-boot-starter
is named in the rule.
1 Create an autoconfiguration module
First create an empty project, and then create a SpringBoot
module, delete the startup class and test class in this class.
(1) Pom configuration
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Only these two dependencies are kept in the pom file, among which spring-boot-starter
are the basic dependencies of the launcher, which spring-boot-configuration-processor
can be prompted when configuring the file:
(2) Write configuration
package com.glp.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "diy.hello")
public class Properties {
private String pre;
private String suf;
public String getPre() {
return pre;
}
public void setPre(String pre) {
this.pre = pre;
}
public String getSuf() {
return suf;
}
public void setSuf(String suf) {
this.suf = suf;
}
}
@ConfigurationProperties
: This configuration class is application.properties/application.yaml
associated with the configuration file in SpringBoot for property injection.
(3) Writing service
package com.glp.service;
import com.glp.properties.Properties;
public class HelloService {
Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String sayHello(){
return properties.getPre()+"offer"+properties.getSuf();
}
}
(4) Automatic configuration
An integration of the service and the configuration class in the automatic configuration class is equivalent to passing the configured class into the service class.
package com.glp.config;
import com.glp.properties.Properties;
import com.glp.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(Properties.class)
public class HelloServiceAutoConfiguration {
@Autowired
Properties properties;
@Bean
public HelloService getHelloService(){
HelloService service = new HelloService();
service.setProperties(properties);
return service;
}
}
@ConditionalOnXXXX
: Used to determine whether certain conditions are met, and to determine whether the automatic configuration takes effect@Bean
: Integrate services and configuration classes, configure them into components, and inject them into the container for users to call.@EnableConfigurationProperties
: Let xxxProperties take effect and add it to the container
(5)spring.factories
Write your own META-INF
file. When the project starts to scan the package, the configuration class specified under the spring.factories file will be automatically loaded.
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.glp.config.HelloServiceAutoConfiguration
2 Create a starter statuser module
This module is only used for transitive dependencies, so it is Maven
enough for us to create a new module.
Only need to introduce our custom in the pom autoconfigure
, it plays a dependent role. Of course if you don’t want toAutomatic configuration code and dependency management Separately, you can also use only one module.
<dependencies>
<dependency>
<groupId>com.glp</groupId>
<artifactId>diy-spring-boot-autoconfiguration</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
Note: To
introduce the automatic configuration class, just copy the groupId, artifactId and version of the automatic configuration class pom.xml.
3 Install the automatic configuration module and startup module into the Maven warehouse
4 Writing test classes
Let's create a new SpringBoot
module test class, and just introduce the Web module into the test class, without introducing too much.
(1) Introduce dependency in the test class:
<dependency>
<groupId>com.glp</groupId>
<artifactId>diy-spring-boot-stater</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
(2) Applicantrion.properties configuration
diy.hello.pre=where
diy.hello.suf=offer
(3) Write a controller:
package com.glp.controller;
import com.glp.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/say")
public String helloController(){
return helloService.sayHello();
}
}
(4) Operation results