In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the difference between spring-boot-starter and custom starter". In daily operation, I believe that many people have doubts about the difference between spring-boot-starter and custom starter. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between spring-boot-starter and custom starter?" Next, please follow the editor to study!
When I talked about sending email with spring-boot-starter-mail earlier, I focused on the convenience of sending email with spring boot. Today, let's talk about another aspect, the structure of spring-boot-starter itself.
1. Check out what's in the official starter jar.
When I used starter before, I was done with it. When I sent an email, I was curious and clicked on the contents of spring-boot-starter-mail 's jar package. I found that there was only one MANIFEST.MF file, no class file, no configuration file, and it was very simple.
Let's take a look at what's in this MANIFEST.MF.
Manifest-Version: 1.0Implementation-Title: Spring Boot Mail StarterAutomatic-Module-Name: spring.boot.starter.mailImplementation-Version: 2.1.8.RELEASEBuilt-By: SpringBuild-Jdk-Spec: 1.8Created-By: Maven Archiver 3.4.0
This is also very ordinary, more ordinary than the mediocre Gu Tianle, which is not scientific. If I could send e-mail only with this file, I would have married Xinyuan knot by collecting photo photos. Make sure the code is somewhere else. Before looking for the code, let's make our own starter.
2. I suddenly want to start writing my own starter.
It's easy to write your own starter. We first download a basic project structure from https://start.spring.io/, and then we need to modify a few things.
First of all, the pom file needs to be modified. My pom file looks like this.
4.0.0 com.skyblue mystarter-spring-boot-starter 1.0 mystarter spring boot starter demo 1.8 org.springframework.boot spring-boot-dependencies 2.1.9.RELEASE pom import org.springframework.boot spring-boot-autoconfigure compile org.projectlombok Lombok 1.18.6 true provided org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
Compared to the original pom.xml, there are several changes.
Mystarter-spring-boot-starter
The official recommendation of spring to write artifactId is as follows
The official naming format is: spring-boot-starter- {name}
Unofficial suggested naming format: {name}-spring-boot-starter
Therefore, the official starter used to send mail is spring-boot-starter-mail, and I use mystarter-spring-boot-starter here.
The original pom.xml will have this paragraph, which needs to be removed, otherwise the classes written by yourself cannot be added to the package, and the jar is full of spring boot classes.
Org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE
At least two dependencies need to be added.
Org.springframework.boot spring-boot-dependencies 2.1.9.RELEASE pom import Org.springframework.boot spring-boot-autoconfigure compile
In fact, it is OK to put both dependencies in the node, and the difference between them can be searched by yourself.
After the pom.xml has been modified, we need to write class for our starter. In order to demonstrate, we will only print two values. Look at the code.
Public interface MyStarterService {String getMessage (); Integer getCode ();} public class MyStarterServiceImpl implements MyStarterService {@ Autowired private MyStarterProperties myStarterProperties; public String getMessage () {return myStarterProperties.getMessage ();} public Integer getCode () {return myStarterProperties.getCode ();}}
The interface and implementation class simply return the property value, and the configuration file for the property value looks like this
@ ConfigurationProperties (prefix = "mystarter") public class MyStarterProperties {String message; int code; public String getMessage () {return message;} public void setMessage (String message) {this.message = message;} public int getCode () {return code } public void setCode (int code) {this.code = code;}}
The @ ConfigurationProperties annotation indicates that the parameters message and code in MyStarterProperties will be read from the configuration file. Prefix = "mystarter" means that the parameter names in the configuration file are prefixed with the prefix mystarter. To take a specific example, for example, the parameter we used to send email is also configured in application.properties. The content of the parameter is like this.
Spring.mail.host=smtp.163.comspring.mail.port=25spring.mail.username=youname@163.comspring.mail.password=yourpassword
Where host,port,username,password is the name of the parameter, and spring.mail is the prefix.
The above is equivalent to the business function part, and now you need to declare the business function into the spring-boot-starter system, depending on the following class
@ Configuration// tells spring container configuration file read MyStarterProperties.class@EnableConfigurationProperties ({MyStarterProperties.class}) / / Import business component MyStarterServiceImpl@Import (MyStarterServiceImpl.class) public class MyStarterAutoConfiguration {}
I use the simplest way, in fact, spring boot also provides @ Conditional series annotations to achieve a more precise configuration of loading Bean conditions, which will not be detailed here.
Finally, we need to tell spring boot where to find the MyStarterAutoConfiguration and create a spring.factories file under resources/META-INF
The content is also very simple, just one sentence.
Org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.skyblue.mystarter.MyStarterAutoConfiguration
In this way, in fact, a custom starter is completed, and a starter can be generated directly with mvn install.
3. Look back at the real implementation code of spring-boot-starter-mail
When naming starter, it is said that the official naming format has a fixed format. In fact, the official convenience is not in the name, but that the code is included in the jar of spring boot. When we introduce the dependency of spring boot, we will automatically load spring-boot-autoconfigure.xxx.jar, open this jar, and you can see the real code of mail.
Do you have a familiar feeling? MailProperties and the above MyStarterProperties,MailSenderAutoConfiguration and the above MyStarterAutoConfiguration are obviously written in accordance with the rules of spring boot starter, except that the official starter code is not placed in starter's jar package, but is packaged in spring-boot-autoconfigure 's jar. If we take a look at the MailSenderAutoConfiguration source code, we can see that it uses @ Configuration, @ EnableConfigurationProperties, @ Import, as well as @ Conditional annotations that we don't use.
@ Configuration@ConditionalOnClass ({MimeMessage.class, MimeType.class, MailSender.class}) @ ConditionalOnMissingBean (MailSender.class) @ Conditional (MailSenderCondition.class) @ EnableConfigurationProperties (MailProperties.class) @ Import ({MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class}) public class MailSenderAutoConfiguration {/ * * Condition to trigger the creation of a {@ link MailSender}. This kicks in if either * the host or jndi name property is set. * / static class MailSenderCondition extends AnyNestedCondition {MailSenderCondition () {super (ConfigurationPhase.PARSE_CONFIGURATION) } @ ConditionalOnProperty (prefix = "spring.mail", name = "host") static class HostProperty {} @ ConditionalOnProperty (prefix = "spring.mail", name = "jndi-name") static class JndiNameProperty {}}
There is also a spring.factories file, which can also be found in spring-boot-autoconfigure.jar
Inside, we can see a complete list of AutoConfiguration classes for the official spring boot starter
# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\ org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\ org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\.
My side is not all listed, according to this to find the need of the official starter is more convenient.
4. When we turn around, we call our custom starter
We create another project with https://start.spring.io/, and then load the starter dependencies in pom.xml
Com.skyblue mystart 1.0 jar system D:\\ workspace\\ mystart\\ target\\ mystarter-spring-boot-starter-1.0.jar
For convenience, I directly used pom.xml to call the locally packaged starter package. If I have a private server of maven, I can introduce it normally. Configure the application.properties file
Mystarter.message=hello Worldwide mystarter.codekeeper 42
Write a class that calls starter
Servicepublic class TestService {@ Resource private MyStarterService myStarterService; public void message () {System.out.println ("code:" + myStarterService.getCode ()); System.out.println ("message:" + myStarterService.getMessage ());}}
Start spring boot to view the results
SpringBootApplicationpublic class StartdemoApplication {public static void main (String [] args) {ApplicationContext context = SpringApplication.run (StartdemoApplication.class, args); ((TestService) context.getBean ("testService")) .message ();}}
Console can see the printed message and code
. _ _ / / _ _ _ (() _ _ _ |'_ _ _ | |\ / _ _ _ | | | (_ _ | |) )'| _ |. _ _ | _ | | _ | _ | | _\ _ _ | / = | _ | = | _ _ / = / _ /:: Spring Boot:: (v2.1.9.RELEASE) 2019-10-10 2222 13 INFO 49.521 INFO 21952-[main] c.w.startdemo.StartdemoApplication: Starting StartdemoApplication on skyblue with PID 21952 (D:\ workspace\ startdemo\ target\ classes started by wphmo in D:\ workspace\ startdemo) 2019-10-10 22:13:49. 527 INFO 21952-[main] c.w.startdemo.StartdemoApplication: No active profile set Falling back to default profiles: default2019-10-22 INFO 13 main 50.405 INFO 21952-[main] c.w.startdemo.StartdemoApplication: Started StartdemoApplication in 1.353 seconds (JVM running for 1.983) code:42message:hello world!
In this way, a complete custom starter runs successfully.
At this point, the study on "what is the difference between spring-boot-starter and custom starter" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.