自定义starter"/>
Spring Boot 自定义starter
Spring Boot 自定义starter
Spring Boot 为大多数流程的开源项目提供了stater包,便于开发者做系统集成。同时,Spring Boot 允许开发者根据业务需要自定义stater,以方便在同一个组织中使用。在之前的公司中,自定义的starter由架构师团队开发维护,他们负责研发公司内部公共组件。
Spring Boot 自动配置机制
Spring Boot 启动时,在classpath中寻找位于 /META-INF目录下的 spring.factories 文件。Spring Boot Auto Configuration 自动装配机制根据以下几种情况确定是否进行装配
- @ConditionalOnProperty - 根据属性判断是否进行装配
- @ConditionalOnClass - 根据类名判断是否进行装配
- @ConditionalOnBean - 根据bean判断是否进行装配
- @ConditionalOnResource - 根据资源判断是否进行装配
以上注解都是 自动装配经常使用的。Spring Boot 自动注解详细介绍请参考
自定义starter
根据官网文档描述,自定义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包,以及自动装配其他模块
首先创建三个模块
- starter-library - 公共的基础模块
- hello-spring-boot-starter-configure - 自动装配模块
- hello-spring-boot-starter - 外部引用模块
公共基础模块
在该模块中,定义了几个基础的类
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;}
自动装配模块
pom依赖
<dependency><groupId>com.andy.spring.boot.custom.starter</groupId><artifactId>starter-library</artifactId><version>1.0-SNAPSHOT</version>
</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 {@Autowiredprivate GreeterProperties greeterProperties;@Bean@ConditionalOnMissingBeanpublic 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@ConditionalOnMissingBeanpublic Greeter greeter(GreetingConfig greetingConfig) {return new Greeter(greetingConfig);}
}
外部引用模块
该模块只包含pom文件依赖,没有任何实际的代码
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>${spring-boot.version}</version></dependency><dependency><groupId>com.andy.spring.boot.custom.starter</groupId><artifactId>hello-spring-boot-starter-configure</artifactId><version>${project.version}</version></dependency><dependency><groupId>com.andy.spring.boot.custom.starter</groupId><artifactId>starter-library</artifactId><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.starter</groupId><artifactId>hello-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>
注入Bean
添加依赖后,开发者可以想使用普通bean注入一样,直接使用@Autowired注入bean
@SpringBootApplication
public class GreeterSampleApplication implements CommandLineRunner {@Autowiredprivate Greeter greeter;public static void main(String[] args) {SpringApplication.run(GreeterSampleApplication.class, args);}@Overridepublic void run(String... args) throws Exception {String message = greeter.greet();System.out.println(message);}
}
源码地址
点击这里
更多推荐
Spring Boot 自定义starter
发布评论