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

An example Analysis of the principle of self-defined starter for springboot Micro Services

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the springboot micro-service custom starter principle example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

After using spring boot to develop micro services, the number of projects has greatly increased (be sure to cut according to the domain, not a middleware client package), so that each jar from the development and runtime self-inclusion has become one of the important content. Spring boot starter can be used to solve this problem (do not rely on applicationContext.getBean to obtain bean to deal with when nothing starts, the dependency relationship is too troublesome, sometimes it is troublesome to solve this problem in complex systems, and you need to modify the open source framework code to achieve it. In turn, after modifying the open source source code, maintenance is also a hassle). Back to the point, let's talk about custom starter. First of all, please be familiar with the core concept of spring boot, otherwise it is easy to starter for the sake of starter.

Create a maven project and add the following dependencies to the pom file:

Org.springframework.boot spring-boot-autoconfigure 2.0.0.RELEASE org.springframework.boot spring-boot-maven-plugin

Create a properties property class to read properties (optional, of course, it's hard to change if you don't follow spring boot autoconfig's routine at first, but once you do, you think, TMD is the real development model, and the @ Value set should have been lost a long time ago).

@ ConfigurationProperties (prefix = "com.xxx") public class HelloServiceProperties {private String name = "james"; private String hobby = "cc"; public String getName () {return name;} public void setName (String name) {this.name = name;} public String getHobby () {return hobby;} public void setHobby (String hobby) {this.hobby = hobby;}}

@ ConfigurationProperties configuration this annotation can automatically import properties in the application.properties configuration file, provided you specify the attribute prefix prefix.

3. Create a configuration class

Public class HelloService {private String name; private String hobby; public String getName () {return "name is" + name;} public String getHobby () {return "hobby is" + hobby;} public void setName (String name) {this.name = name;} public void setHobby (String hobby) {this.hobby = hobby;}}

4. Create an automatic configuration class:

@ Configuration@EnableConfigurationProperties (HelloServiceProperties.class) @ ConditionalOnClass (HelloServiceConfiguration.class) @ ConditionalOnProperty (prefix = "com.xxx", value = "enabled", matchIfMissing = true) @ ComponentScan ({"com.xxx"}) / / if there are too many bean, public class HelloServiceAutoConfiguration {@ Autowired private HelloServiceProperties helloServiceProperties is generally used. @ Bean / / modules with few bean and special requirements for sequence and logic are generally used in this way: @ ConditionalOnMissingBean (HelloServiceConfiguration.class) public HelloServiceConfiguration helloServiceConfiguration () {HelloService helloService = new HelloService (); helloService.setName (helloServiceProperties.getName ()); helloService.setHobby (helloServiceProperties.getHobby ()); return helloService;}}

5. Create a new META-INF file under the resources folder, and create a spring.factories file below to register the configuration classes in 4.

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxx.HelloServiceAutoConfiguration

6. Create a new springboot project and add the coordinates of the jar you just packaged in the pom file.

7. Use the @ Autowired provider.

@ SpringBootApplication@RestControllerpublic class Springboot03Application {@ Autowired private HelloService helloService; public static void main (String [] args) {SpringApplication.run (Springboot03Application.class, args);} @ RequestMapping ("/ name") public String getName () {return helloService.getName ();} @ RequestMapping ("/ hobby") public String getHobby () {return helloService.getHobby ();}}

Using autoconfigure starter makes the application significantly more self-contained and decoupled than using the @ Import annotation to import a @ Configuration class, or maintaining all paths to ComponentScan in one place.

Thank you for reading this article carefully. I hope the article "sample Analysis of the Custom starter principle of SpringBoot Micro Service" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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