I’ve written dozens of Spring Boot series articles, including Spring Boot Starters, using, and introducing third-party Starters, How do you develop your own Spring Boot Starter?
The following take you to develop a young person’s first Spring Boot Starter!
For those of you who don’t know the Starters, please go ahead and enter the Spring Boot Starters portal. With that in mind, let’s move on to the next tutorial on custom Starters!
1. Customize the required components of the Starter
A complete Spring Boot Starter needs to contain the following components:
- An auto-configuration module containing auto-configuration code; Reference: Spring Boot automatic configuration principle, actual combat.
- The Starter module provides dependencies on automatic modules, libraries of related dependencies, and any dependencies that need to be used. In short, adding a Starter should provide everything you need to use that Starter;
Create a custom Starter
How to create a Spring Boot project will not say, before also shared, reference: young people’s first Spring Boot application! .
The custom Starter implements a Bean that is configured based on the value of the property.
1. Create the automatic configuration class
package cn.javastack.springboot.starter.config;
import cn.javastack.springboot.starter.service.TestService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnProperty(prefix = "javastack.starter", name = "enabled", havingValue = "true")
public class TestServiceAutoConfiguration {
@Bean
public TestService testService() {
returnnew TestService(); }}Copy the code
The automatic configuration class is very simple, is that according to whether have javastack. Starter. Enabled = true on the value of this parameter to configure a Bean.
The following is an example of TestService:
package cn.javastack.springboot.starter.service;
public class TestService {
public String getServiceName() {
return "Java Technology Stack"; }}Copy the code
This class has a method getServiceName that returns a string: Java technology stack.
2. Allow automatic configuration
Create a meta-INF /spring.factories file and add the class that allows automatic configuration.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.javastack.springboot.starter.config.TestServiceAutoConfiguration
Copy the code
Test the custom Starter
Once the custom Starter project above is built, you can test it to see if it works.
It is usually uploaded to the Maven repository as a JAR package for other colleagues to call, here we will reference it after the package is finished.
1. Add dependencies
Create a New Spring Boot test project and add the custom Starter dependency as follows:
<dependencies> <dependency> <groupId>cn.javastack</groupId> <artifactId>javastack-spring-boot-starter</artifactId> The < version > 1.0 < / version > < scope > compile < / scope > < / dependency > < / dependencies >Copy the code
2. Add test methods
package cn.javastack.springboot.starter.sample; import cn.javastack.springboot.starter.service.TestService; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; /** * wechat official number: Java stack */ @springBootApplication public class Application {public static void main(String[] args) { SpringApplication.run(Application.class); } @Bean public CommandLineRunnercommandLineRunner(TestService testService) {
return (args) -> {
System.out.println(testService.getServiceName()); }; }}Copy the code
This method calls the TestService instance method and outputs the value of the method after the project is started.
For CommandLineRunner, see this article: Spring Boot Runner.
3. Enable the configuration
We know the custom of the Starter need javastack. Starter. Enabled = true on the value of this parameter, so we in the application. The yml configuration files to add this configuration:
javastack:
starter:
enabled: true
Copy the code
4. Run tests
Running the Main method of the Application class yields the Java technology stack.
When we change the configuration to:
javastack:
starter:
enabled: false
Copy the code
The autoconfiguration class will only be configured if it is true.
Four,
This chapter briefly demonstrates how to customize a Spring Boot Starter to automatically configure it based on the value of a parameter, including a class, a Bean… ConditionOnXXX, ConditionOnXXX, ConditionOnXXX, ConditionOnXXX, ConditionOnXXX, ConditionOnXXX
Now that you know how Spring Boot works, it’s not difficult to customize a Starter, and you can extend it flexibly based on this example.
Complete code example of this article Github address:
Github.com/javastacks/…
Java technology stack, has updated a lot of tutorials, this tutorial will continue to update…