When we use SpringBoot starter, such as MyBatis, we just need to introduce the relevant starter of MyBatis, and then write the configuration in the configuration file. Before, we need to configure the configuration file of MyBatis, as well as a variety of configurations, so it is much easier
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> The < version > 2.1.3 < / version > < / dependency >Copy the code
Starter is so useful that we can build our own. Start by creating a SpringBoot project and import a dependency in the pop.xml file
<dependency> <groupId>org.springframework.boot</groupId> <dependency> <groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency>Copy the code
2: Write the attribute source
/ / @configurationProperties (prefix = "coco") public class StarterSource {/ Type =123 * coke. rate=123 */ private String type; private String rate; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getRate() { return rate; } public void setRate(String rate) { this.rate = rate; }}Copy the code
3: write service classes to provide interface services to the outside world
*/ public class StartService {private StarterSource StarterSource; public StartService(StarterSource starterSource) { this.starterSource = starterSource; } @return */ public String getType() {return startersource.getType (); } public String getRate() { return starterSource.getRate(); }}Copy the code
4: Write the attribute auto-configuration class
/ attribute automatically configure class * * * * / @ Configuration @ EnableConfigurationProperties (StarterSource. Class) / * * * there is Configuration of the weather. If the enable and its value ConditionalOnProperty(name = "weather. Enable ",havingValue = "enable") public class StarterAutoConfiguration { @Autowired private StarterSource starterSource; ConditionalOnMissingBean(startService.class) public StartService startService() { return new StartService(starterSource); }}Copy the code
“> < div style =” box-type: initial; box-type: initial
// Configure our own attribute auto-configuration class org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.starter.StarterAutoConfigurationCopy the code
6: Install a Maven help plugin. After the plugin is installed, right-click the project Run Maven-> Clean Install and import the dependency in another SpringBoot project
7. Prepare for the test:
Coke. type= Java coke. rate=spark Enable =enable Create a new Controller @autoWired private StartService StartService; @GetMapping("/test") public String test() { return startService.getType(); }Copy the code