In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to customize starter in SpringBoot, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1 create an empty project first (name yourself)
2 create two modules in the empty project created in step 1, as shown in the following figure
1) create the initiator module with the maven project my initiator name is mao-spring-boot-starter
The pom.xml is as follows
4.0.0com.mao.startermao-spring-boot-starter1.0-SNAPSHOT UTF-8 UTF-8 1.8 com.mao-starter mao-spring-boot-starter-autoconfigurer 0.0.1-SNAPSHOT
2) create an automatic configuration module. The name I created is: mao-spring-boot-starter-autoconfigurer, as shown in the following figure! [] (https://oscimg.oschina.net/oscnet/b7c0a779402394cb5783f3f2eae7a5ce0bc.jpg)pom.xml is shown below
4.0.0 org.springframework.boot spring-boot-starter-parent 1.5.21.RELEASE com.mao-startermao-spring-boot-starter-autoconfigurer0.0.1-SNAPSHOTmao-spring-boot-starter-autoconfigurerDemo project for Spring Boot UTF-8 UTF-8 1.8
Org.springframework.boot spring-boot-starter
3 create three classes in the following figure
HelloProperties.java / / attribute class
Package com.maostarter.mao
Import org.springframework.boot.context.properties.ConfigurationProperties
@ ConfigurationProperties (prefix = "map.hello")
Public class helloProperties {
Public String prefix;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;} private String suffix
}
HelloService.java / / classes that provide services to the outside
Public class HelloService {
HelloProperties helloPropertie;public helloProperties getHelloPropertie () {return helloPropertie;} public void setHelloPropertie (helloProperties helloPropertie) {this.helloPropertie = helloPropertie;} public String sayHelloMao (String name) {return helloPropertie.getPrefix () +'*'+ name+helloPropertie.getSuffix ();}
}
HelloServiceAutoConfiguration.java / / Service automatic injection class
@ Configuration
@ ConditionalOnWebApplication / / web application takes effect
@ EnableConfigurationProperties (helloProperties.class)
Public class helloServiceAutoConfiguration {
@ AutowiredhelloProperties helloPropertie;@Beanpublic HelloService helloService () {HelloService service = new HelloService (); service.setHelloPropertie (helloPropertie); return service;}
}
4 configure spring.factories file, as shown in the following figure
Spring.factories
Org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
Com.maostarter.mao.helloServiceAutoConfiguration
5 how to generate starter, there are various problems in this process, such as the wrong version of jdk and the problem of maven version. If there are problems, first read the console information carefully.
Don't go to Baidu whenever something goes wrong.
6 Test the custom starter you just created to create a springBoot project
The pom.xml is as follows
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE com.autostart.testautostart0.0.1-SNAPSHOTautostartDemo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web com .mao.starter mao-spring-boot-starter 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-test test Org.springframework.boot spring-boot-maven-plugin
6 copy the previously configured properties file as shown below
7 create a test case
@ Controller
Public class HelloController {
@ Autowired HelloService helloService; @ ResponseBody@RequestMapping ("/ hello") public String Hello () {return helloService.sayHelloMao ("");}
}
8 the test results are shown in the following figure
The above is how to customize starter in SpringBoot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.