自定义的spring autoconfig starter
Springboot 自定义starter步骤
- 创建一个自定义配置工程,设置相关配置类及对应的
spring.factories
文件org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ ${指定配置类}
- 在其他服务中引用此starter
1. 创建一个自定义配置工程
<?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>
<packaging>pom</packaging>
<groupId>pr.iceworld.fernando</groupId>
<version>0.0.1</version>
<artifactId>basic-springboot</artifactId>
<modules>
<module>fruit-autoconfig-starter</module>
<module>fruit-service</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.cloud-version>2021.0.4</spring.cloud-version>
<spring.boot-version>2.7.5</spring.boot-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
1.1 创建一个自定义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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>basic-springboot</artifactId>
<groupId>pr.iceworld.fernando</groupId>
<version>0.0.1</version>
</parent>
<artifactId>fruit-autoconfig-starter</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
创建一个水果接口,及水果实现类
package pr.iceworld.fernando.fruit.service;
public interface Fruit {
void eat();
}
package pr.iceworld.fernando.fruit.service;
public class Apple implements Fruit {
@Override
public void eat() {
System.out.println("eat apple");
}
}
package pr.iceworld.fernando.fruit.service;
public class Orange implements Fruit {
@Override
public void eat() {
System.out.println("eat orange");
}
}
Vegetable
主要用于可自动加载Apple
Bean
package pr.iceworld.fernando.fruit.service;
public class Vegetable {
}
定义配置类,默认条件加载
package pr.iceworld.fernando.fruit.autoconfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pr.iceworld.fernando.fruit.service.Apple;
import pr.iceworld.fernando.fruit.service.Fruit;
import pr.iceworld.fernando.fruit.service.Orange;
import pr.iceworld.fernando.fruit.service.Vegetable;
@Configuration
@ConditionalOnClass(Fruit.class)
public class FruitConfiguration {
/**
* no bean of type Orange is already contained in the BeanFactory.
*/
@Bean
@ConditionalOnMissingBean
public Orange orange() {
System.out.println("loading orange");
return new Orange();
}
/**
* a bean of type Apple is already contained in the BeanFactory.
*/
@Bean
@ConditionalOnBean(Vegetable.class)
public Apple apple() {
System.out.println("loading apple");
return new Apple();
}
}
定义META-INF/spring.factories
,用于@SpringBootApplication
启动时能找到对应的自定义starter
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
pr.iceworld.fernando.fruit.autoconfig.FruitConfiguration
2. 在其他服务中引用此工程
<?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>
<artifactId>basic-springboot</artifactId>
<groupId>pr.iceworld.fernando</groupId>
<version>0.0.1</version>
</parent>
<artifactId>fruit-service</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>pr.iceworld.fernando</groupId>
<artifactId>fruit-autoconfig-starter</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>
package pr.iceworld.fernando.fruit;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import pr.iceworld.fernando.fruit.service.Fruit;
@SpringBootApplication
public class FruitMainApplication {
public static void main(String[] args) {
SpringApplication.run(FruitMainApplication.class, args);
}
/**
* 使用`Fruit` bean 注入
*/
@Bean
public CommandLineRunner commandLineRunner(Fruit fruit) {
return args -> { fruit.eat(); };
}
/**
// 如果此服务申明了`Fruit`bean,单没有使用@Primary做修饰,也可在使用`Fruit`bean地方指明需要使用哪一个
@Bean
public CommandLineRunner commandLineRunner(@Qualifier("pear") Fruit fruit) {
return args -> { fruit.eat(); };
}
*/
}
2.1 默认没有定义 Fruit
Bean
为了方便看日志输出,将日志级别改为 logging.level.root=warn
@Configuration
public class ProjectConfiguration {
}
没加载Apple
bean
loading orange
eat orange
2.2 申明Fruit
bean
package pr.iceworld.fernando.fruit.service;
public class Pear implements Fruit {
@Override
public void eat() {
System.out.println("eat pear");
}
}
@Configuration
public class ProjectConfiguration {
@Bean
/**
* 不使用这个修饰符,会导致spring bean 判定不出应该使用哪一个`Fruit` bean
*/
@Primary
public Fruit pear() {
System.out.println("loading pear");
return new Pear();
}
}
没加载Apple
bean
loading pear
loading orange
eat pear
2.3 class Apple
加载
@Configuration
public class ProjectConfiguration {
@Bean
@Primary
public Fruit pear() {
System.out.println("loading pear");
return new Pear();
}
/**
* 通过申明一个`Vegtable` bean, `Apple`就可以加装了
*/
@Bean
public Vegetable vegetable() {
return new Vegetable();
}
}
加载Apple
bean
loading pear
loading orange
loading apple
eat pear
可能出现的错误
如果服务中也定义了Fruit
bean 但无使用@Primary
修饰 或者没有在使用 Fruit
的申明中指定哪一个bean 会出现以下错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method commandLineRunner in pr.iceworld.fernando.fruit.FruitMainApplication required a single bean, but 2 were found:
- pear: defined by method 'pear' in class path resource [pr/iceworld/fernando/fruit/autoconfig/ProjectConfiguration.class]
- orange: defined by method 'orange' in class path resource [pr/iceworld/fernando/fruit/autoconfig/FruitConfiguration.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
注意事项
此处自定义starter命名并不符合spring建议,如需使用spring命名建议,参考
https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/html/boot-features-developing-auto-configuration.html#boot-features-custom-starter-naming