Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Spring Boot customizes starter

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Spring Boot how to customize starter, the article is very detailed, has a certain reference value, interested friends must read it!

I. brief introduction

The most powerful function of SpringBoot is to extract our commonly used scenes into starter (scene initiators). By introducing these scene initiators provided to me by springboot, we can use the corresponding functions with a small amount of configuration. Even so, springboot doesn't cover all of our usage scenarios, and often we need custom starter to simplify our use of springboot.

Second, how to customize starter1. How to write automatic configuration for an instance?

Let's take @ WebMvcAutoConfiguration as an example to see what you need to prepare. here is some of the code for WebMvcAutoConfiguration:

Configuration@ConditionalOnWebApplication@ConditionalOnClass ({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class}) @ ConditionalOnMissingBean ({WebMvcConfigurationSupport.class}) @ AutoConfigureOrder (- 2147483638) @ AutoConfigureAfter ({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class}) public class WebMvcAutoConfiguration {@ Import ({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}) @ EnableConfigurationProperties ({WebMvcProperties.class) ResourceProperties.class}) public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {@ Bean @ ConditionalOnBean ({View.class}) @ ConditionalOnMissingBean public BeanNameViewResolver beanNameViewResolver () {BeanNameViewResolver resolver = new BeanNameViewResolver () Resolver.setOrder (2147483637); return resolver;}}

We can extract some of the same configurations that we also need when we customize starter.

@ Configuration / / specify that this class is a configuration class @ ConditionalOnXXX / / specify that the automatic configuration class takes effect if the condition is true @ AutoConfigureOrder / / specify the order of the automatic configuration class @ Bean / / add components @ ConfigurationProperties / / bind the related configuration @ EnableConfigurationProperties / / combine the related xxxProperties to bind the relevant configuration @ EnableConfigurationProperties / / add the automatic configuration class to the container to be able to load the automatic configuration class Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ mode in META-INF/spring.factories

We looked at spring-boot-starter and found that there was no code:

We have a springboot-starter in the dependency in its pom.

Org.springframework.boot spring-boot-starter

Let's take another look. Spring-boot-starter has a spring-boot-autoconfigure.

Org.springframework.boot spring-boot-autoconfigure

Some of the automatic configuration of web is written here, so we have a summary:

Initiator starter is only used for dependency management. When you need to write a configuration module similar to spring-boot-autoconfigure, you only need to introduce initiator starter, and you can use the auto-configured naming convention.

Official namespace

Prefix: spring-boot-starter-

Mode: spring-boot-starter- module name

For example: spring-boot-starter-web, spring-boot-starter-jdbc

Custom namespace

Suffix:-spring-boot-starter

Mode: module-spring-boot-starter

For example: mybatis-spring-boot-starter

3. Custom starter instance

We need to create two projects hello-spring-boot-starter and hello-spring-boot-starter-autoconfigurer first

1. Hello-spring-boot-starter1.pom.xml 4.0.0 com.gf hello-spring-boot-starter 0.0.1-SNAPSHOT jar hello-spring-boot-starter com.gf hello-spring-boot-starter-autoconfigurer 0.0.1-SNAPSHOT

At the same time, delete the startup class, files under resources, and test files.

2. Hello-spring-boot-starter-autoconfigurer1. Pom.xml 4.0.0 com.gf hello-spring-boot-starter-autoconfigurer 0.0.1-SNAPSHOT jar hello-spring-boot-starter-autoconfigurer Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 Org.springframework.boot spring-boot-starter 2. HelloPropertiespackage com.gf.service Import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties (prefix = "gf.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 3. HelloServicepackage com.gf.service;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 ();}} 4. HelloServiceAutoConfigurationpackage com.gf.service;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 should take effect @ EnableConfigurationProperties (HelloProperties.class) public class HelloServiceAutoConfiguration {@ Autowired HelloProperties helloProperties; @ Bean public HelloService helloService () {HelloService service = new HelloService (); service.setHelloProperties (helloProperties); return service;}} 5. Spring.factories

Create the folder META-INF under resources and the file spring.factories under META-INF, as follows:

# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.gf.service.HelloServiceAutoConfiguration

At this point, our configuration custom starter has been written, and our hello-spring-boot-starter-autoconfigurer, hello-spring-boot-starter installation cost jar package.

3. Test the custom starter

Let's create a project hello-spring-boot-starter-test to test the stater we wrote.

1. Pom.xml 4.0.0 com.gf hello-spring-boot-starter-test 0.0.1-SNAPSHOT jar hello-spring-boot-starter-test Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 Org.springframework.boot spring-boot-starter-web com.gf hello-spring-boot-starter 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test test Org.springframework.boot spring-boot-maven-plugin 2. HelloControllerpackage com.gf.controller Import com.gf.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {@ Autowired HelloService helloService; @ GetMapping ("/ hello/ {name}") public String hello (@ PathVariable (value = "name") String name) {return helloService.sayHello (name + ",") 3. Application.propertiesgf.hello.prefix = higf.hello.suffix = what's up man?

I run the project to access http://127.0.0.1:8080/hello/zhangsan, and the result is as follows:

Hi-zhangsan, what's up man? The above is all the content of this article "how to customize starter by Spring Boot". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report