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

What is the principle of Spring Boot starter?

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

Share

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

This article mainly explains "what is the principle of Spring Boot starter". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the principle of Spring Boot starter"?

Spring Boot starter principle

Spring Boot divides common development functions into starter, so that when we develop functions, we only need to introduce the corresponding starter, instead of introducing a bunch of dependencies! Starter can be understood as a dependency group, and its main function is to introduce 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 class

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;}} 3. Add a method to concatenate 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;}} IV. New 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;}} 5. New 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.organization.JavatipAutoConfiguration VI. Package 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:

@ RestControllerpublic 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.

Thank you for your reading, the above is the content of "what is the principle of Spring Boot starter", after the study of this article, I believe you have a deeper understanding of what the principle of Spring Boot starter is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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