基於SpringBoot自定義第三方starter
阿新 • • 發佈:2021-01-27
經過上篇文章瞭解了spring自動配置的原理後,我們可以自定義第三方starter,在pom檔案中引入依賴就自動注入到IOC容器中.
主要分為以下幾步:
1.建立自動配置工程與起步工程,
2.在自動配置工程中建立配置類與要注入的類
package com.hy.config;
import com.hy.data.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework. context.annotation.Bean;
/**
* @author HY
* @ClassName MyAutoConfiguration
* @Description TODE
* @DateTime 2021/1/25 22:08
* Version 1.0
*/
public class MyAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name="data")
public User getUser() {
return new User();
}
}
User:
package com.hy.data;
/**
* @author HY
* @ClassName User
* @Description TODE
* @DateTime 2021/1/25 22:12
* Version 1.0
*/
public class User {
}
3.在resources下建立配置檔案META-INF/spring.factories,並且講上面自定義的配置類的全限定名寫入。
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration= \
com.hy.config.MyAutoConfiguration
關於什麼時候載入spring.factories以及過程請看這篇文章
4.在起步工程中引入自動配置工程的依賴
自動配置工程的依賴:
<?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>
<groupId>com.hy</groupId>
<artifactId>myproject-spring-boot-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
<!--起步依賴-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
起步工程依賴自動配置工程
<?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>
<groupId>com.hy</groupId>
<artifactId>myproject-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.hy</groupId>
<artifactId>myproject-spring-boot-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
5.測試
新建一個測試工程,在pom檔案中引入starter:
<dependencies>
<dependency>
<groupId>com.hy</groupId>
<artifactId>myproject-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
我沒有手動注入bean,看下能不能獲取到bean:
package com.hy.enable;
import com.hy.config.MyAutoConfiguration;
import com.hy.data.User;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
/**
* @author HY
* @ClassName SpringApplication
* @Description TODE
* @DateTime 2021/1/25 22:47
* Version 1.0
*/
@SpringBootApplication
public class SpringApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = org.springframework.boot.SpringApplication.run(SpringApplication.class, args);
User bean = run.getBean(User.class);
System.out.println(bean);
}
}
輸出結果:
okkkkkk!!!