Spring Boot Starter 简介
Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件)进行自动配置。使用者只需要依赖相应功能的Starter,无需做过多的配置和依赖,Spring Boot就能自动扫描并加载相应的模块。
上步骤
1、新建项目
项目结构如下:
2、新建一个maven 项目,关键依赖 spring-boot-starter,自动配置spring-boot-configuration-processor。
<?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 https://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.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itlong</groupId>
<artifactId>woody-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>woody-spring-boot-starter</name>
<description>woody-spring-boot-starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
3、新建自动装配类,使用@Configuration
和@Bean
来进行自动装配,@EnableConfigurationProperties 装载配置属性,@ConditionalOnProperty(value = "woodyeap.enabled",havingValue = "true") 指定自动配置类的条件
package com.itlong.woodyspringbootstarter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
@EnableConfigurationProperties(WoodyEapProperties.class)
@ConditionalOnProperty(value = "woodyeap.enabled",havingValue = "true")
public class WoodyEapAutoConfiguration {
@Autowired
private WoodyEapProperties woodyEapProperties;
@Bean
public WoodyServer getBean(WoodyEapProperties config){
log.info("已开始注册bean:WoodyServer");
WoodyServer woodyServer=new WoodyServer();
woodyServer.setUsername(config.getUsername());
woodyServer.setPassword(config.getPassword());
return woodyServer;
}
@Bean
public WoodyClient getBean2(){
log.info("开始注册bean:WoodyClient");
WoodyClient woodyClient=new WoodyClient();
woodyClient.setUsername (woodyEapProperties.getUsername());
woodyClient.setPassword(woodyEapProperties.getPassword());
return woodyClient;
}
}
4、新建配置类,写好配置项和默认的配置值,指明配置项前缀
package com.itlong.woodyspringbootstarter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@Slf4j
@ConfigurationProperties(prefix = "woodyeap")
public class WoodyEapProperties {
private String username="itl123";//默认值
private String password="itl123456";
}
5、自动装载的类
package com.itlong.woodyspringbootstarter;
import lombok.Data;
@Data
public class WoodyServer {
private String username;
private String password;
}
6、新建spring.factories文件,指定Starter的自动装配类。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.itlong.woodyspringbootstarter.WoodyEapAutoConfiguration
spring.factories文件位于resources/META-INF目录下,需要手动创建;
org.springframework.boot.autoconfigure.EnableAutoConfiguration
后面的类名说明了自动装配类,如果有多个 ,则用逗号分开;
使用者应用(SpringBoot)在启动的时候,会通过org.springframework.core.io.support.SpringFactoriesLoader
读取classpath下每个Starter的spring.factories文件,加载自动装配类进行Bean的自动装配;
到此,整个Starter包开发完成,通过Deploy或install到相应的仓库即可使用。
使用Starter
项目结构
1、创建spring boot 项目,引入依赖刚才发布的woody-spring-boot-starter。
2、编写应用程序启动类,注入Bean 使用
3、控制台观察
4、结果