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 do a SpringBoot Starter with bare hands

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

Share

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

This article will explain in detail how to masturbate a Starter with bare hands. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Spring Boot starter principle

Spring Boot divides the common development functions into starter, so that when we develop the function, we only need to introduce the corresponding starter instead of a bunch of dependencies! starter can be understood as a dependency group, and its main function is to complete the introduction of dependencies and initialize the configuration. The official naming convention for starter provided by Spring is spring-boot-starter-xxx, and the naming convention for starter provided by third parties is xxx-spring-boot-starter.

Here we use RocketMQ's dependence on rocketmq-spring-boot-starter to learn the principle of starter.

After introducing rocketmq-spring-boot-starter into the project, some related dependencies of rocketmq are actually introduced.

There is a self-assembling class RocketMQAutoConfiguration in rocketmq-spring-boot, and I intercepted a small piece of code to take a look at it.

Configuration @ EnableConfigurationProperties (RocketMQProperties.class) @ ConditionalOnClass ({MQAdmin.class}) @ ConditionalOnProperty (prefix = "rocketmq", value = "name-server", matchIfMissing = true) @ Import ({MessageConverterConfiguration.class, ListenerContainerConfiguration.class, ExtProducerResetConfiguration.class, RocketMQTransactionConfiguration.class}) @ AutoConfigureAfter ({MessageConverterConfiguration.class}) @ AutoConfigureBefore ({RocketMQTransactionConfiguration.class}) public class RocketMQAutoConfiguration {private static final Logger log = LoggerFactory.getLogger (RocketMQAutoConfiguration.class); public static final String ROCKETMQ_TEMPLATE_DEFAULT_GLOBAL_NAME = "rocketMQTemplate" @ Autowired private Environment environment; @ Bean (destroyMethod = "destroy") @ ConditionalOnBean (DefaultMQProducer.class) @ ConditionalOnMissingBean (name = ROCKETMQ_TEMPLATE_DEFAULT_GLOBAL_NAME) public RocketMQTemplate rocketMQTemplate (DefaultMQProducer mqProducer, RocketMQMessageConverter rocketMQMessageConverter) {RocketMQTemplate rocketMQTemplate = new RocketMQTemplate (); rocketMQTemplate.setProducer (mqProducer); rocketMQTemplate.setMessageConverter (rocketMQMessageConverter.getMessageConverter ()); return rocketMQTemplate;}}

@ Configuration indicates that this is a configuration class, and the method annotated by @ Bean in the class is a bean of spring, such as rocketMQTemplate.

@ EnableConfigurationProperties, which enables bean with @ ConfigurationProperties, where RocketMQProperties is introduced.

RocketMQProperties is the attribute that needs to be written in the yml file.

ConfigurationProperties (prefix = "rocketmq") public class RocketMQProperties {private String nameServer; private String accessChannel; private Producer producer; private Consumer consumer = new Consumer ();}

When the Spring Boot project starts, only classes with @ Configuration annotations are scanned by default, so how is RocketMQAutoConfiguration scanned like the one mentioned in this article? In fact, when the project starts, it will load all the spring.factories files in the project, and then load the corresponding configuration classes, so we need to specify only the classes that need to be scanned in the spring.factories.

Now that the principle is clear, let's simply implement a starter of our own! The main function of this starter is to concatenate a string at the end of an object!

I. New projects

Create a new project named javatip-spring-boot-starter and introduce the following dependencies

Org.springframework.boot spring-boot-starter

II. New configuration classes

The attribute in the properties file corresponding to the configuration class is javatip.name

@ ConfigurationProperties (prefix = "javatip") public class JavatipPorperties {private String name; public String getName () {return name;} public void setName (String name) {this.name = name;}}

Third, add the method of concatenating strings

The main purpose of this method is to concatenate a fixed string for the object

Public class StrUt {private String name; public String strTo (Object object) {return object + "- -" + getName ();} public String getName () {return name;} public void setName (String name) {this.name = name;}}

Fourth, add automatic configuration class

Use the annotation @ EnableConfigurationProperties to enable the JavatipProperties configuration class

Use the annotation @ Configuration with @ Bean to register a bean object that concatenates strings.

@ Configuration @ EnableConfigurationProperties (JavatipPorperties.class) public class JavatipAutoConfiguration {@ Autowired private JavatipPorperties javatipPorperties; @ Bean public StrUt strut () {StrUt strut = new StrUt (); strut.setName (javatipPorperties.getName ()); return strut;}}

Add configuration Discovery File

Create a new META-INF folder in the resources folder, create a new configuration discovery file spring.factories in the META-INF folder, and write the automatic configuration class to the file.

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.javatip.str.configuration.JavatipAutoConfiguration

VI. Packaging testing

Use the mvn install command to package and push the project to the local maven repository, and then create a new test project to introduce packaged dependencies.

Com.javatip javatip-spring-boot-starter 0.0.1-SNAPSHOT

Write the attribute javatip.name for the automatically concatenated string in the application.yml file.

Javatip: name: Java Journey

Then handwrite a test class:

@ RestController public class Test {@ Autowired private StrUt strUt; @ GetMapping ("test") public String test () {String str = strUt.strTo ("who are you?"); return str;}}

After running the test class, the page returns

Who are you?---Java Journey

In this way, a simple starter is written, and as long as you understand the principle of starter, it is very easy to implement. The first point is that starter is equivalent to a dependent group, and the other is that starter can complete the initialization configuration.

About how to do a Starter with bare hands to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 299

*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