I. Overview:
The power of the Spring Boot is the ability to extract the scene as a starter
as long as the loaded launcher to quickly configure the appropriate scene
Then step Custom Starter (Scene initiator) are:
1 , the first scene to determine the required dependencies what is
2 , then write autoconfiguration
Use @Configuration
annotation to specify a class configuration class
Use @ConditionalOnXXX
annotations that automatically configures the class will take effect in the case of a specified condition is met
example:
@ConditionalOnWebApplication(
type = Type.SERVLET
)
Use @AutoConfigurationAfter
annotation to specify the order automatically configure class
Example:
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
Use @Bean
annotations to add components to a container
Use @ConfigurationProperties
annotations associated binding xxxProperties class configuration corresponding to bind
Example:
@ConfigurationProperties(
prefix = "spring.mvc"
)
Use @EnableConfigurationProperties
annotations make xxxProperties class into force and added to the container
example:
@EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
Must in the classpath META-INF
under spring.factories file specified class path automatically configured
so that SpringBoot know automatically configured to load at startup class of which
cases:
# Initializers
org.springframework.context.ApplicationContextInitializer=\
net.zjitc.springboot.listener.HelloApplicationContextInitializer
# Listener
org.springframework.boot.SpringApplicationRunListener=\
net.zjitc.springboot.listener.HelloSpringApplicationRunListener
Precautions: a starter should be empty jar file only provide supplementary dependency managing those dependencies can be used in other libraries or automatic assembly
and an automatic configuration module specifically written initiator is dependent on the automatic configuration module
therefore only introduced promoter It is deemed introduced automatic configuration module
that is: xxx-starter (initiator) → xxx-starter-autoconfigurer (auto configuration module)
Naming convention :
- SpringBoot official namespace:
prefix: "spring-boot-starter-"
mode:spring-boot-starter-模块名
Example: spring-boot-starter-web - Custom namespace:
suffix: "- spring-boot-starter "
mode:模块名-spring-boot-starter
Example: mybatis-spring-boot-starter
Second, the project created
Because of the need to start and auto-configuration module therefore these two convenience to create an empty project in IDEA for
Click the plus sign to add modules
to select new module
Then create a Maven project and a project SpringBoot
Maven project:
SpringBoot project:
Maven project as a starter SpringBoot works as automatic configuration module
Third, the configuration
1, first of all depend on good pom configuration file
Starter project pom files need to introduce automatic configuration module:
<!-- 启动器 -->
<dependencies>
<!-- 引入自动配置模块 -->
<dependency>
<groupId>net.zjitc.starter</groupId>
<artifactId>zjitc-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
Auto-configuration module pom file must have introduced spring-boot-starter:
Because the spring-boot-starter is the basic configuration of all starter will need to be introduced
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
2, followed by a write auto-configuration module:
HelloProperties.java
Class attributes :
Attribute class defined parameter autoconfiguration requires all the attributes used
in the class can be used @ConfigurationProperties
annotation specified prefix
so can be designated with the prefix to modify the parameters in the configuration file
@ConfigurationProperties(prefix = "zjitc.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;
}
}
HelloService.java
Class method
which provides a method to be performed:
public class HelloService {
// 需要创建配置类对象 因为要使用配置类中的参数
HelloProperties helloProperties;
public String sayHello(String name)
{
// 使用配置类中的参数
return helloProperties.getPrefix()+name+helloProperties.getSuffix();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
HelloServiceAutoConfiguration.java
Automatic configuration class
This class is a class automatically configured but first class have to be configured so use @Configuration
annotations to specify
@ConditionalOnWebApplication
annotations represent only take effect is a Web application
@EnableConfigurationProperties
notes gives the specified attribute class attribute passed, to enable it to take effect the attribute class to class
in the class of the method of the classes configuration initialization :
// 自动配置类
@Configuration
// 只有Web应用才会生效
@ConditionalOnWebApplication
// 让属性类中的属性生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService()
{
HelloService helloService=new HelloService();
// 设置属性配置文件
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
Then configure /METE-INF/spring.factories
profile
disposed in the configuration file path automatically configured at start loading classes :
# 指定启动时要加载的自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.zjitc.starter.HelloServiceAutoConfiguration
Finally directory like this:
Enter the install Maven lifecycle will automatically configure the module and the starters are installed into the warehouse
after installation in the warehouse can be used directly in other projects to introduce coordinates
Fourth, the test
Then create a project for testing:
only need to introduce coordinates starter can:
because all depend starter will automatically configure all import
<!-- 引入自定义的starter -->
<dependency>
<groupId>net.zjitc.zjitc</groupId>
<artifactId>zjitc-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Then it simple
Test categories:
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("hello")
public String hello()
{
return helloService.sayHello("Piconjo");
}
}
Can be introduced into the launcher class has been described starter introduced
Since the test method calls need to use these two properties and two properties and no pre-configured initial value
and therefore need to be configured in a test project at the attributes:
# 配置前后缀
zjitc.hello.prefix=ZJITC
zjitc.hello.suffix=SOFTWARE1801
Test success