SpringBoot自定义starter
源码地址:https://github.com/YuSheng1223/SpringBoot
1.为什么要自定义starter?
SpringBoot提供的自动配置功能为我们构建工程和开发程序节省了大量的时间。作为一个码农,光会用可是不够的。
这里我们尝试着自己去定义一个starter,在平时工作中,一个自定义的starter可以帮我们完成一些SpringBoot不支持组件的初始化工作,并且理解原理也能让我们面试大大加分。
2.如何自定义starter?
1.新建SpringBoot项目
这里就不放图了,按照我们平时新建工程的步骤来就可以了。关于命名尽量遵循格式。
自定义starter命名方式:
官方 spring-boot-starter-模块名
-
非官方(如我们自己编写的) 模块名-spring-boot-starter
2.pom中引入自动配置jar包
首先需要在pom文件中引入我们需要的jar包,如果没有其他需要,引入spring-boot-autoconfigure
,spring-boot-starter-web
这两个就足够了。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
将其他多余的jar包删除,不仅占用空间,而且还有可能引起错误。
spring-boot-maven-plugin
这个插件是一定要移除的。因为当我们打包的时候,MAVEN会将编译完的class文件放到BOOT-INF/classes/
路径下,从而导致引用starter的工程无法从jar包的根路径下找到需要自动配置的类,程序启动失败。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.加载配置文件内容的类
这里需要写一个在配置文件中加载属性的类,从而让我们可以显式的进行配置。
import org.springframework.boot.context.properties.ConfigurationProperties;
//指定从配置文件中加载属性的前缀
@ConfigurationProperties(prefix = "print")
public class PrintProperties {
private String text;
public void setText(String text){
this.text = text;
}
public String getText(){
return this.text;
}
}
4.要自动配置的类
public class PrintUtils {
private static final Logger logger = LoggerFactory.getLogger(PrintUtils.class);
private final String text;
public PrintUtils(String text){
this.text = text;
}
public void printText(){
logger.info(" starter print text : {} ",text);
}
}
这个类就是我们要进行自动配置的类,类似于RedisTemplate、JdbcTemplate等。如果想要使用,从spring容器中直接获取就可以了。
5.完成自动配置初始化的类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//开启自动配置
@EnableConfigurationProperties({PrintProperties.class})
public class PrintAutoConfiguration {
@Autowired
private PrintProperties printProperties;
@Bean
@ConditionalOnProperty(prefix = "print.config", name = "enable", havingValue = "true")
public PrintUtils InitializePrintUtils(){
return new PrintUtils(this.printProperties.getText());
}
}
上面的 @ConditionalOnProperty,使得如果想要使用此starter,必须显式的指定print.config.enable=true才可以。
这个类开启了PrintProperties类的属性加载功能,从PrintProperties类中获取属性,将属性配置到PrintUtils类,并且将PrintUtils类注入到spring容器中。
6.让其他工程扫描到此starter的自动配置
新建META-INF/spring.factories文件,并且在spring.factories文件中指定需要开启自动配置的类。
注意spring.factories文件的位置。
spring.factories文件中的内容
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xs.starter.PrintAutoConfiguration
7.打包
这一步就不多说了,我们将此工程打成jar包就可以了。
8.在其他工程中引用starter
现在就可以正式使用我们自定义的starter了。首先引入自定义starter的jar包。
<dependency>
<groupId>com.xs</groupId>
<artifactId>spring-boot-custom-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在yaml或者properties文件中进行配置:
print:
config:
enable: true //开启配置
text: "海上月是天上月" //要打印的文本
在代码中使用:
@Slf4j
@Controller
public class HelloController {
//从容器中拿出我们注入的PrintUtils
@Autowired
PrintUtils printUtils;
@RequestMapping("/hello03")
public void testStarter(){
printUtils.printText();
}
}
请求/hello03,就可以从控制台看到打印了 starter print text : 海上月是天上月
,说明我们自定义的starter生效了。
更多技术文章、技术交流群微信搜索: java晋升