In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I introduce a Starter of SpringBoot and write my own Starter by hand. In the SpringBoot project, there are all kinds of Starter for developers to use, and Starter provides all kinds of API, which makes it easy to develop SpringBoot projects. In fact, Starter is simply developed by Spring+SpringMVC. Don't say much and start playing with the code.
1. Create a project
First create the SpringBoot project in idea, and first create a BeautyProperties class with the following code:
Package com.mystarter;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties (prefix = "beauty") public class BeautyProperties {private String name; private Integer age; public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age }} @ ConfigurationProperties (prefix = "beauty") annotation indicates that the variable values defined in the application.properties file in the resource directory with the beauty prefix are mapped to this class, and the object is assigned to the XXProperties class. If programmers who have read the SpringBoot source code know that Starter has various XXProperties classes corresponding to .properties files in the SpringBooot source code, as shown in the figure, all the automatic configuration-related values are under the spring-boot-autoconfigure jar package. Two configuration classes for RabbitProperties, BatchProperties, and so on are listed.
Click into the RabbitProperties class to see, the code is as follows, only part of the paste, this class is also using the @ ConfigurationProperties annotation to map the values in the configuration file with the attribute values in this class, the purpose is to assign these attributes to leave a question here, why don't the bosses find the answer by themselves? Where are the files that map to these classes? Baidu Ha @ ConfigurationProperties (prefix = "spring.rabbitmq") public class RabbitProperties {private String host = "localhost"; private int port = 5672 [private String username = "guest"; private String password = "guest"; private final RabbitProperties.Ssl ssl = new RabbitProperties.Ssl (); private String virtualHost;private String addresses; then creates an ActionService class, which has nothing to say, as follows: package com.mystarter
Public class ActionService {
Private String name;private Integer age;public String sayHello () {return "my name is + name +", I am "+ age +" years old ";} public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age;} public String getName () {return name;} public void setName (String name) {this.name = name;}
}
-finally, create a class ActionServiceAutoConfiguration, which is the key. The code is as follows:-@ Configuration annotation indicates that this is a configuration class-@ EnableConfigurationProperties (BeautyProperties.class) indicates that @ ConfigurationProperties annotation is enabled to make the annotation take effect. @ ConditionalOnClass (ActionService.class) conditional judgment annotation indicates that the condition takes effect only when this class ActionService is present, that is, the configuration takes effect. -automatically inject BeautyProperties classes into the IOC container through @ Autowired.-@ Bean injects the returned value into the container.
Package com.mystarter
Import org.springframework.beans.factory.annotation.Autowired
Import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
Import org.springframework.boot.context.properties.EnableConfigurationProperties
Import org.springframework.context.annotation.Bean
Import org.springframework.context.annotation.Configuration
@ Configuration@EnableConfigurationProperties (BeautyProperties.class) br/ > @ EnableConfigurationProperties (BeautyProperties.class)
Public class ActionServiceAutoConfiguration {
@ AutowiredBeautyProperties beautyProperties;@BeanActionService helloService () {ActionService helloService = new ActionService (); helloService.setName (beautyProperties.getName ()); helloService.setAge (beautyProperties.getAge ()); return helloService;}
}
-then in the application.properties file under the resources folder, add the following configuration, which is injected into the configuration class as the default value when using this Starter when no relevant value is set.
Beauty.name= Li Yiyi acquiesced
Beauty.age=18
-finally, create a new META-INF folder in resources, and then create a new file spring.factories. The name of this file and the name of the folder cannot be changed. The configuration is as follows. This indicates that the full path of the class that is configured automatically is specified, and the full path is found when automatically configured, and the object is instantiated to the container.
Org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mystarter.ActionServiceAutoConfiguration
-Click install in the last step, and the Build Success appears indicating that the Starter has been installed in the local maven repository and can be referenced by others! [insert picture description here] (https://img-blog.csdnimg.cn/20191218230620617.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70)## 2. Test Starter and create a new SpringBoot project, and add the following configuration to the application.properties file:
Beauty.name= Li Yiyi
Beauty.age=24
Introduce dependencies into the pom file as follows:
Com.org.ldc
Mystarter
1.0-SNAPSHOT
Then test it with the following code
Package com.org.ldc.mystarter
Import com.mystarter.HelloService
Import org.junit.jupiter.api.Test
Import org.springframework.beans.factory.annotation.Autowired
Import org.springframework.boot.test.context.SpringBootTest
Import org.springframework.test.context.junit4.SpringRunner
@ SpringBootTest
Class TestmystarterApplicationTests {
AutowiredHelloService helloService;@Testpublic void contextLoads () {System.out.println (helloService.sayHello ());}
}
Execute the test, which appears as follows, indicating that the creation is successful! [insert picture description here] (https://img-blog.csdnimg.cn/2019121823094633.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70)> for more tutorials, please follow: non-class classes, please click on the bosses who are free to pass by, thank you
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.