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 to develop Spring Boot Starter

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

Share

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

This article mainly shows you "how to develop Spring Boot Starter", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to develop Spring Boot Starter" this article.

About Starter

Spring Boot adheres to the development mode of "convention is greater than configuration", which makes our Spring Boot-based development project very efficient. I believe that partners who have used Spring Boot will find that when we want to use a component provided by Spring, we only need to add the component's starter dependency in the pom.xml file to integrate into the project.

For example, adding spring-boot-starter-web dependencies to the pom.xml file allows the project to integrate the functionality of Spring MVC. And almost no configuration is needed under the simplest use, but in the past, if you want to integrate Spring MVC, you not only need to add a bunch of related dependent packages such as spring-web, spring-webmvc, and so on, but also complete many complicated configurations to achieve integration.

This is because starter has helped us integrate various dependency packages to avoid missing dependency packages or version conflicts between dependency packages. And completed a lot of basic configuration and automatic assembly, so that we can skip most of the configuration under the simplest use, so as to achieve the effect of out-of-the-box. This is also one of the cores of Spring Boot's implementation of "convention is greater than configuration".

Develop a Starter by hand

Through the above description, we can simply regard starter as a modular package with large functional granularity of a component, including the integration, basic configuration and automatic assembly of the required dependency packages.

In addition to the official starter provided by Spring, we can also develop a starter based on our business. For example, when the project is accumulated to a certain extent, we can sink some common functions into a starter. Developing a starter is also simple, with the following steps:

Create a new Maven project and define the required dependencies in the pom.xml file

Create a new configuration class, write the configuration item and default value, and use @ ConfigurationProperties to indicate the configuration item prefix

Create a new auto-assembly class and use @ Configuration and @ Bean for auto-assembly

Create a new spring.factories file to specify the path to the autoassembly class

Install starter into the maven repository so that other projects can refer to

Next, take encapsulating a starter for operating redis as an example to show the implementation of these steps step by step. The first step is to create a new maven project. The complete pom.xml content is as follows:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.0.RELEASE com.example spring-boot-starter-demo 0.0.1-SNAPSHOT spring-boot-starter-demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter Org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage Junit-vintage-engine redis.clients jedis 3.1.0 com.google.code.gson gson 2.8.6

The second step is to create a new property configuration class and write the configuration items and default values. And use @ ConfigurationProperties to indicate the configuration item prefix, which is used to load the prefix configuration item corresponding to the configuration file:

Package com.example.starter.demo.properties;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;/** * attribute configuration class, which is used to load the prefix configuration item * * @ author zero * @ date 2019-11-04 * * / @ Data@ConfigurationProperties ("demo.redis") public class RedisProperties {private String host = "127.0.0.1"; private int port = 6379; private int timeout = 2000; private int maxIdle = 5 Private int maxTotal = 10; private long maxWaitMillis = 10000; private String password;}

Write a simple redis operation tool with the following code:

Package com.example.starter.demo.component;import com.google.gson.Gson;import lombok.RequiredArgsConstructor;import lombok.extern.slf4j.Slf4j;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;/** * redis Operation component * * @ author zero * @ date 2019-11-04 * * / @ Slf4j@RequiredArgsConstructorpublic class RedisComponent {private final JedisPool jedisPool / * get value with key * / public T get (String key, Class clazz) {try (Jedis resource = jedisPool.getResource ()) {String str = resource.get (key); return stringToBean (str, clazz) }} / * set value with key * / public boolean set (String key, T value, int expireSeconds) {try (Jedis resource = jedisPool.getResource ()) {String valueStr = beanToString (value); if (valueStr = = null | | valueStr.length () = 0) {return false;} if (expireSeconds)

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