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

Springboot Custom starter initiator how to use

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

Share

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

This article mainly shows you "springboot custom starter launcher how to use", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "springboot custom starter launcher how to use" this article.

The first step is to create the spring Initializr module of xxx-spring-boot-starter

The second step is to delete the unwanted content (startup class, maven compilation plug-in except for the following spring-boot-starter dependencies) org.springframework.boot spring-boot-starter

The following is the complete pom.xml

In fact, if the current starter needs to reference other dependencies to add to the dependences, only the demonstration project is done here.

4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE top.huashengshu my-spring-boot-starter 0.0.1-SNAPSHOT my-spring-boot-starter Demo project for Spring Boot 11 org.springframework.boot spring-boot-starter

Screenshot of project structure

The third step is to write code and provide some self-written classes.

Create HelloProperties.java, copy the following code directly, and then select the package to paste. Idea will automatically create the corresponding class code and set the package name.

The prefix provided by import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties (prefix = "hello") / / is equivalent to the other prefixes introduced into the current starter to use hello in the properties file. Attribute can assign the following attributes public class HelloProperties {private String prefix; / / member attribute, meaning the prefix name private String suffix; / / member attribute, which means the suffix name 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;}}

Create a HelloService.java and directly copy the following code, select the package and paste it to generate

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 ();}}

Create a configuration class (just copy and paste as before) HelloServiceAutoConfiguration.java to inject HelloService into the IOC container

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 / / conditional configuration class. This annotation indicates that it only takes effect in the web environment. Other related conditions can be used @ ConditionXXX@EnableConfigurationProperties (HelloProperties.class) / / to indicate that HelloProperties uses public class HelloServiceAutoConfiguration {@ Autowired HelloProperties helloProperties as the configuration class. / / the purpose of the configuration class is to prefix and suffix @ Bean public HelloService helloService () {/ / inject HelloService into the IOC container HelloService service = new HelloService (); service.setHelloProperties (helloProperties); return service;}} the fourth step is to create a META-INF folder under the resources resource folder and create a spring.factories file

Take a screenshot as below

The content is to add the @ Configuration configuration class to add the configuration to the external IOC container

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=

Right-click copy- "copy reference in idea and fill in the copied value above = right

Note: if there is more than one AutoConfiguration, separate it with a comma, and enter to be careful of the space in front of you, preferably without other characters.

For example:

Step 5, org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ top.huashengshu.myspringbootstarter.HelloServiceAutoConfiguration,\ top.yumbo.music.starter.configuration.YumboMusicAutoConfiguration, release the maven repository of the project, or install it into the local warehouse so that other projects can use the

Local installation as an example:

It can be done after success.

Step 6, test the effectiveness of the self-defined initiator

Create a springboot project

Check the web module, and then add the gav dependency of the custom initiator

Add an inner class to the startup class (here to demonstrate that the package is not created according to the specification)

The following sample code

Startup class

Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import top.huashengshu.myspringbootstarter.HelloService;@SpringBootApplicationpublic class DemoApplication {@ RestController public class HelloController {@ Autowired HelloService helloService / / inject HelloService @ GetMapping ("/ hello") / / expose a / hello request path to provide services public String hello () {return helloService.sayHello ("zhang san"); / / return a string}} public static void main (String [] args) {SpringApplication.run (DemoApplication.class, args);}} with a prefix and "zhang san" in the middle.

Properties file

Because the @ ConfigurationProperties (prefix = "hello") annotation is used, the member attribute can be assigned using the hello prefix call in the properties file of the current project

As follows

Hello.prefix=HUASHENGSHUhello.suffix=Hello World

Run the current project and visit / hello to verify that it is valid

As follows:

Indicates that the custom starter was successful.

Other business code, according to their own needs to join the dependency, that is, you can define your own starter to provide to others!

The above is all the content of the article "how to use springboot Custom starter launcher". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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