table of Contents
Description
The launcher module is an empty jar file that only provides auxiliary dependency management. These dependencies may be used for automatic assembly or other class libraries;
Name reduction:
Official naming:
-
Prefix: spring-boot-starter-xxx
-
For example: spring-boot-starter-web...
Custom naming:
-
xxx-spring-boot-starter
-
For example: mybatis-spring-boot-starter
Write a launcher
1. Create a new empty project spring-boot-starter-diy in IDEA
2. Create a new ordinary Maven module: kuang-spring-boot-starter
3. Create a new Springboot module: kuang-spring-boot-starter-autoconfigure
4. Import the dependency of autoconfigure in our starter!
<!-- 启动器 -->
<dependencies>
<!-- 引入自动配置模块 -->
<dependency>
<groupId>com.kuang</groupId>
<artifactId>kuang-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
5. Delete all the redundant files under the autoconfigure project. Only one starter is left in the Pom, which is the basic configuration of all starters!
6. Write your own service
package com.kuang;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix() + name + helloProperties.getSuffix();
}
}
7, write HelloProperties configuration class
package com.kuang;
import org.springframework.boot.context.properties.ConfigurationProperties;
// 前缀 kuang.hello
@ConfigurationProperties(prefix = "kuang.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
8. Write our automatic configuration class and inject beans, test!
package com.kuang;
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 //web应用生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
9. Write your own META-INF\spring.factories in resources
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kuang.HelloServiceAutoConfiguration
10. After the writing is completed, it can be installed in the maven warehouse!
test
1. Create a new SpringBoot project
2. Import our own launcher
<dependency>
<groupId>com.kuang</groupId>
<artifactId>kuang-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
3. Write a HelloController to test our own written interface!
package com.kuang.controller;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String hello(){
return helloService.sayHello("zxc");
}
}
4. Write the configuration file application.properties
kuang.hello.prefix="ppp"
kuang.hello.suffix="sss"
5. Start the project to test, the result is successful!