目标:实现自定义spring-boot-starter封装
目录
②、配置appllication.yml 或 application.properties
1、Starter封装
可以使用IDE工具直接创建一个SpringBoot工程或者maven工程
①、pom 依赖:
<?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.7.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ap</groupId>
<artifactId>custom-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 自动装配核心依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 配置元信息 idea代码提示用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
②、属性定义(Properties)
package com.ap.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 类名:ExampleProperties.java
* 描述:
*
* @author AP
* @version 1.0
* @date 2023/7/12 10:02
*/
@ConfigurationProperties(prefix = ExampleProperties.PREFIX )
public class ExampleProperties {
public static final String PREFIX = "com.ap";
/**
* 名称
*/
private String name;
/**
* 版本
*/
private String version;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
③、自动配置 (AutoConfiguration)
package com.ap.config;
/**
* 类名:ExampleAutoConfiguration.java
* 描述:
*
* @author AP
* @version 1.0
* @date 2023/7/12 10:12
*/
import com.ap.module.ExampleModule;
import com.ap.properties.ExampleProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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
@EnableConfigurationProperties(ExampleProperties.class)
public class ExampleAutoConfiguration {
/**
* 加载配置信息
* @param properties 属性
* @return 配置类
*
*/
/*
* 个性化加载配置
* @ConditionalOnClass 应用中包含某个类时,对应的配置才生效。
* @ConditionalOnMissingClass 应用中不包含某个类时,对应的配置才生效。
* @ConditionalOnBeanSpring 容器中存在指定class的实例对象时,对应的配置才生效。
* @ConditionalOnMissingBeanSpring 容器中不存在指定class的实例对象时,对应的配置才生效。条件依赖
* @ConditionalOnProperty 指定参数的值符合要求时,对应的配置才生效。
* @ConditionalOnResource 指定文件资源存在时,对应的配置才生效。
* @ConditionalOnWebApplication 当前处于Web环境时(WebApplicationContext),对应的配置才生效。
* @ConditionalOnNotWebApplication 当前非处于Web环境时,对应的配置才生效。
* @ConditionalOnExpression 指定参数的值符合要求时,对应的配置才生效。和ConditionalOnProperty的区别在于这个注解使用springEL表达式。
*
* @AutoConfigureAfter 在指定的Configuration类之后加载先后顺序
* @AutoConfigureBefore 在指定的Configuration类之前加载
* @AutoConfigureOrder 指定该Configuration类的加载顺序,默认值0
*/
@Bean("exampleModule")
@ConditionalOnMissingBean
public ExampleModule exampleModule(ExampleProperties properties){
ExampleModule exampleModule = new ExampleModule();
exampleModule.setName(properties.getName());
exampleModule.setVersion(properties.getVersion());
return exampleModule;
}
}
④、配置生效 【被动/主动方式】
被动方式:创建 spring.factories 资源文件【resources/META-INFO/】
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ap.config.ExampleAutoConfiguration
主动方式: 需要集成使用方主动声明使用 @import(ExampleAutoConfiguration.class)
详见 2-3 服务使用
⑤、封包 install
2、Starter使用
创建一个springboot web项目
①、pom 引入封装的starter
<?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.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ap</groupId>
<artifactId>use-example-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>use-example-starter</name>
<description>use-example-starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入封装的starter -->
<dependency>
<groupId>com.ap</groupId>
<artifactId>example-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
②、配置appllication.yml 或 application.properties
com:
ap:
name: useExampleStarter
version: 1.0.0
com.ap.name="useExampleStarter"
com.ap.version="1.0.0"
③、服务使用
package com.ap1;
import com.ap.module.ExampleModule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@SpringBootApplication
//@Import(ExampleAutoConfiguration.class)
@RestController
public class UseExampleStarterApplication {
public static void main(String[] args) {
SpringApplication.run(UseExampleStarterApplication.class, args);
}
@Resource
private ExampleModule exampleModule;
@GetMapping("/use")
public String useExampleStarter() {
return exampleModule.toString();
}
}