Spring Boot 为大多数流程的开源项目提供了stater包,便于开发者做系统集成。同时,Spring Boot 允许开发者根据业务需要自定义stater,以方便在同一个组织中使用。在之前的公司中,自定义的starter由架构师团队开发维护,他们负责研发公司内部公共组件。
Spring Boot 启动时,在classpath中寻找位于 /META-INF目录下的 spring.factories 文件。Spring Boot Auto Configuration 自动装配机制根据以下几种情况确定是否进行装配
以上注解都是 自动装配经常使用的。Spring Boot 自动注解详细介绍请参考
根据官网文档描述,自定义Spring Boot starter 需要包含两部分
An auto-configure class for our library along with a properties class for custom configuration.
自定义jar包内部,根据配置属性自动装配的class文件
A starter pom to bring in the dependencies of the library and the autoconfigure project.
一个引用的starter项目,引入依赖jar包,以及自动装配其他模块
首先创建三个模块
在该模块中,定义了几个基础的类
public class Greeter {
private GreetingConfig greetingConfig;
public Greeter(GreetingConfig greetingConfig) {
this.greetingConfig = greetingConfig;
}
public String greet(LocalDateTime localDateTime) {
String name = greetingConfig.getProperty(USER_NAME);
int hourOfDay = localDateTime.getHour();
if (hourOfDay >= 5 && hourOfDay < 12) {
return String.format("Hello %s, %s", name, greetingConfig.get(MORNING_MESSAGE));
} else if (hourOfDay >= 12 && hourOfDay < 17) {
return String.format("Hello %s, %s", name, greetingConfig.get(AFTERNOON_MESSAGE));
} else if (hourOfDay >= 17 && hourOfDay < 20) {
return String.format("Hello %s, %s", name, greetingConfig.get(EVENING_MESSAGE));
} else {
return String.format("Hello %s, %s", name, greetingConfig.get(NIGHT_MESSAGE));
}
}
public String greet() {
return greet(LocalDateTime.now());
}
}
public class GreeterConfigParams {
public static final String USER_NAME = "user.name";
public static final String MORNING_MESSAGE = "morning.message";
public static final String AFTERNOON_MESSAGE = "afternoon.message";
public static final String EVENING_MESSAGE = "evening.message";
public static final String NIGHT_MESSAGE = "night.message";
}
public class GreetingConfig extends Properties{
private static final long serialVersionUID = 5662570853707247891L;
}
<dependency>
<groupId>com.andy.spring.boot.custom.startergroupId>
<artifactId>starter-libraryartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
@ConfigurationProperties(prefix = "com.greeter")
@Data
@ToString
public class GreeterProperties {
private String userName;
private String morningMessage;
private String afternoonMessage;
private String eveningMessage;
private String nightMessage;
}
@Configuration
@ConditionalOnClass(Greeter.class)
@EnableConfigurationProperties(GreeterProperties.class)
public class GreeterAutoConfiguration {
@Autowired
private GreeterProperties greeterProperties;
@Bean
@ConditionalOnMissingBean
public GreetingConfig greeterConfig() {
String userName = greeterProperties.getUserName() == null
? System.getProperty("user.name")
: greeterProperties.getUserName();
// ..
GreetingConfig greetingConfig = new GreetingConfig();
greetingConfig.put(USER_NAME, userName);
// ...
return greetingConfig;
}
@Bean
@ConditionalOnMissingBean
public Greeter greeter(GreetingConfig greetingConfig) {
return new Greeter(greetingConfig);
}
}
该模块只包含pom文件依赖,没有任何实际的代码
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>${spring-boot.version}version>
dependency>
<dependency>
<groupId>com.andy.spring.boot.custom.startergroupId>
<artifactId>hello-spring-boot-starter-configureartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.andy.spring.boot.custom.startergroupId>
<artifactId>starter-libraryartifactId>
<version>${project.version}version>
dependency>
dependencies>
本地另外创建一个项目,在application.properties文件中增加
# 自定义 stater 配置
baeldung.greeter.userName=Baeldung
baeldung.greeter.afternoonMessage=Woha\ Afternoon
在pom文件中添加
<dependency>
<groupId>com.andy.spring.boot.custom.startergroupId>
<artifactId>hello-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
添加依赖后,开发者可以想使用普通bean注入一样,直接使用@Autowired注入bean
@SpringBootApplication
public class GreeterSampleApplication implements CommandLineRunner {
@Autowired
private Greeter greeter;
public static void main(String[] args) {
SpringApplication.run(GreeterSampleApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String message = greeter.greet();
System.out.println(message);
}
}