SpringBoot自定義Starter
再面試的時候被問到會不會自定義Starter 尷尬了。。所以回來百度了下 也就自己寫了下
此Starter 主要功能是自動引入服務(wù) 并且 自動引入Controller 話不多說。
首先創(chuàng)建一個maven 項(xiàng)目 pom 如下所示 :
主要 使用的 是 autoconfigure 依賴包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zyh</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>mybatis-spring-boot-starter</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
接下來 創(chuàng)建 配置類
package com.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.girlfriend")
public class GirlFriendProperties {
private String message ="hello";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
接下來創(chuàng)建一個 接口 重要負(fù)責(zé)輸出我們配置類里面的屬性值
package com.test;
public interface GirlFriendServiceInterface {
void say();
}
接下來創(chuàng)建接口實(shí)現(xiàn)類
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
public class GirlFriendServiceImpl implements GirlFriendServiceInterface{
@Autowired
private GirlFriendProperties girlFriendProperties;
@Override
public void say() {
System.out.println("調(diào)用到了service"+girlFriendProperties.getMessage());
}
}
接下來創(chuàng)建配配置類
@Configuration
@ConditionalOnClass(GirlFriendServiceInterface.class)
@EnableConfigurationProperties(GirlFriendProperties.class)
@Import({TestController.class})
public class GirlFriendConfigration {
@Bean
@ConditionalOnMissingBean
public GirlFriendServiceInterface girlFriendServiceInterface() {
return new GirlFriendServiceImpl();
}
}
Controller 如下所示 :
package com.test.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("test")
public String test() {
return "hello";
}
}
接下來也是最重要的配置了 是如何讓其他項(xiàng)目能夠自動引入我們的Starter呢
有兩種方式
第一種方式 :
在 resources 目錄下面 創(chuàng)建 META-INF 文件夾 并在文件夾下面創(chuàng)建 文件 spring.factories 加入如下代碼:
# 指定剛剛創(chuàng)建的 GirlFriendAutoConfiguration 的全路徑名
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.test.GirlFriendConfigration
第二種方式 :
創(chuàng)建 EnableXXX 注解 推薦大家采用這種方式 因?yàn)楹莒`活 代碼如下:
package com.test;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Import(GirlFriendConfigration.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableSelfConfig {
}
上面的方法采取一個去使用即可 mvn clean install 會生成 jar包
接下來 我們創(chuàng)建一個測試的項(xiàng)目 進(jìn)行測試就可以了 記得講我們自己寫的Starter 依賴進(jìn)來
如果采用了 第一種配置方式 測試項(xiàng)目的啟動類如下:
@SpringBootApplication
public class TestApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class,args);
}
@Autowired
private GirlFriendServiceInterface girlFriendServiceInterface;
public void run(String... args) throws Exception {
girlFriendServiceInterface.say();
}
}
如果采用了 第二種配置方式 就可以使用 EnableXXX 的方式 進(jìn)行配置了
@EnableSelfConfig
@SpringBootApplication
public class TestApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class,args);
}
@Autowired
private GirlFriendServiceInterface girlFriendServiceInterface;
public void run(String... args) throws Exception {
girlFriendServiceInterface.say();
}
}
接下來 給大家 看下結(jié)果的截圖吧
項(xiàng)目啟動后會自動調(diào)用我們Starter里面寫的服務(wù)的方法。

以及Controller。

GitHub 地址如下 :https://github.com/KingestCode/mybatisspringbootstarter
大家可以去down下來看下 。有不足的地方希望大家指出 很多注解的意思我沒給大家解釋 不會的自己百度下吧。