目录
如有错误,希望大家批评指正,日拱一卒,功不唐捐。
前言
SpringBoot相对于SpringMVC最大的优点就是使用简单,即约定大于配置。SpringBoot项目中的最重要的组件就是各种各样的starter。为了更深入理解SpringBoot中starter组件的原理,手动实现一个SpringBoot starter。
一、SpringBoot中的Starter是什么?
springboot中starter的作用是引入依赖的jar包以及自己自定义配置的jar包。也就是把相关的配置封装在starter中,在使用的时候直接通过注解的方式进行Bean的注入即可获取,极大简化了开发流程。
二、SpringBoot中的starter命名规范
spring官方的starter的规范是:spring-boot-starter-模块名-版本号.jar
第三方的starter的规范是:模块名-spring-boot-starter-版本号.jar
三、自定义实现starter
1.目录结构
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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>demo-spring-boot-starter</artifactId>
<version>0.0.1-RELEASE</version>
<name>demo-spring-boot-starter</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
3.properties文件
重要的注解是:@ConfigurationProperties(prefix = “demo”)这个注解就是后续引入该starter的jar包在applicationcontext.properties中配置项的前缀。
package com.yue.starter.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
private String hello;
private String world;
public void setHello(String hello) {
this.hello = hello;
}
public void setWorld(String world) {
this.world = world;
}
public String getHello() {
return hello;
}
public String getWorld() {
return world;
}
}
4.Service类
就是简单的service类
package com.yue.starter.service;
public class DemoService {
public String hello;
public String world;
public DemoService(String hello, String world){
this.hello = hello;
this.world = world;
}
public String say(){
return this.hello + "! " + world;
}
}
5.properties类对应的config类
@Configuration 表这是一个配置类
@EnableConfigurationProperties注解的作用是:让使用了 @ConfigurationProperties 注解的类生效,并且将该类注入到 IOC 容器中,交由 IOC 容器进行管理,也就是对应的DemoProperties类。
@ConditionalOnProperty这个注解的作用是只有配置文件中有下面的配置,且与havingValue的值相等时,才可以生效。
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnProperty(
prefix = "demo",
name = "isopen",
havingValue = "true"
)
public class DemoConfig {
@Autowired
private DemoProperties demoProperties;
@Bean(name = "demo")
public DemoService demoService(){
return new DemoService(demoProperties.getHello(), demoProperties.getWorld());
}
}
6.配置文件
在resource目录下新建META-INF文件夹,并新建文件spring.factories,在配置文件中开启对于DemoConfig的自动配置。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yue.starter.config.DemoConfig
然后用maven打包测试即可。
在实际中遇到的问题:
- 编写application.properties时,idea对于ConditionalOnProperty注解下的demo.isopen注解无提示,这是正常现象,直接手敲即可。
- DemoConfig上的@EnableConfigurationProperties注解其实就是把DemoProperties对应的bean注入到Spring的容器中,那么换个角度,也可以不在DemoConfig类上使用@EnableConfigurationProperties注解,可以直接在DemoProperties上使用@Component注解,实现DemoProperties类对应bean的注入,经过实际测试,效果与使用@EnableConfigurationProperties注解一致。
总结
参考网址:https://www.cnblogs.com/xiaomaomao/p/13934688.html
https://www.cnblogs.com/hello-shf/p/10864977.html
侵权必删