Spring Boot 中自定义 starter

所谓的 starter ,其实就是一个普通的 Maven 项目。

1、首先创建一个普通的 Maven 项目,并引入自动化配置依赖:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-autoconfigure</artifactId>
	<version>2.1.8.RELEASE</version>
	<!-- 表明不传递spring-boot-autoconfigure依赖 -->
    <optional>true</optional>
</dependency>

注意:在自定义的starter 的pom中,将spring-boot-autoconfigure的maven依赖声明为<optional>true</optional>,表明自定义的starter的jar包不会传递spring-boot-autoconfigure依赖;否则会将spring-boot-autoconfigure版本固定,导致引用自定义starter的应用出现版本冲突问题。

starter命名模式 --> ${module}-spring-boot-starter;

例如:

<groupId>com.hello</groupId>
<artifactId>helloService-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
2、创建一个配置类 HelloProperties 来接受 application.properties 中注入的值:
@ConfigurationProperties(prefix = "javaboy")
public class HelloProperties {
	privatestaticfinal String DEFAULT_NAME = "默认名";
	privatestaticfinal String DEFAULT_MSG = "默认消息";
	private String name = DEFAULT_NAME;
	private String msg = DEFAULT_MSG;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
}
3、创建一个服务类 HelloService :
public class HelloService {
	private String msg;
	private String name;
	
	public String sayHello() {
		return name + " say " + msg + " !";
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
4、自定义自动配置类

SpringBoot自动装配的命名规则:

  • 自动装配类应命名为:XxxAutoConfiguration

  • 自动装配package命名模式: ${root-package}.autoconfigure.${module-package},比如:

    org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
    
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {

	@Autowired
	HelloProperties helloProperties;

	@Bean
	HelloService helloService() {
		HelloService helloService = new HelloService();
		helloService.setName(helloProperties.getName());
		helloService.setMsg(helloProperties.getMsg());
		return helloService;
	}
}

注解 @EnableConfigurationProperties 使我们之前配置的 @ConfigurationProperties 生效,让配置的属性注入到 Bean 中。

注解 @ConditionalOnClass 表示当项目的 classpath 下存在 HelloService 时,后面的配置才生效。

5、创建 spring.factories 文件

在resources目录下新建 META-INF/spring.factories 文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
com.hello.autoconfigure.HelloServiceAutoConfiguration

这里的key是固定的,value是我们的自动配置类的全路径。

SpringBoot 项目启动时,会做自动配置,会去扫描所有jar包中的 META-INF/spring.factories 配置文件。

至此,自定义的starter已完成。将这个自动化配置类的项目安装到maven仓库中,然后在其他项目中引入依赖就可以使用。

新建一个普通的 Spring Boot 工程,并加入我们自定义的 starter 的依赖:

<dependency>
	<groupId>com.hello</groupId>
	<artifactId>helloService-spring-boot-starter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

此时我们项目中已有一个 helloService 实例可以使用,而且我们还可以配置它的属性数据,例如,在 application.properties 中配置:

javaboy.name=我的名字
javaboy.msg=提示消息

我们做个单元测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MystarterApplicationTest {

	@Autowired
	private HelloService helloService;
	
	@Test
	public void contextLoads() {
		System.out.println(helloService.sayHello());
	}
}

控制台会打印:我的名字 say 提示消息 !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值