本文介绍
咱们都晓得Spring Boot我的项目由一个个的starter形成,比方web的starter,redis的starter等等。
咱们通过maven引入对应的starter,简直毋庸配置就能够完满应用集成的spring mvc或redis.
那么咱们如何自定义一个starter呢?
本文用将示如何应用SpringBoot的Auto-Configuration个性创立本人的Spring Boot Starter。
假如Toy
为咱们须要集成为starter框架或者类,并且心愿在利用启动的时候就加载到Spring上下文中供咱们调用。
有的小伙伴可能会问为什么要这样做?间接将Toy申明为Spring的bean不就好了。
答案必定是能够这样做,不过秉承Spring Boot疾速上手的准则,通过引入starter的形式能够更加疾速应用集成的咱们想要的性能或者个性,就好比下面的Toy
。
又比方mybatis,因为咱们无奈批改mybatis源码,路径之一是通过@Bean
的形式在办法上创立SqlSessionFactory
bean,并在Spring Boot利用中应用。
路径二则是通过创立starter,须要用到mybatis的中央引入mybatis的starter就可实现mybatis的集成。
在很多我的项目须要集成mybatis的状况下,starter的形式会更为疾速高效。
我的项目构造
本我的项目分为两个模块:
toy-sample-app
和toy-spring-boot-starter
- toy-sample-app: 演示如何引入本人创立的starter
- toy-spring-boot-starter: starter模块,用于构建
Toy
Spring Bean 供演示我的项目应用
代码介绍
toy-spring-boot-starter
- META-INF/spring.factories文件
该文件指明了主动注入的的类名,Spring Boot利用启动的时候会将该类初始化并加载到上下文中。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.bugkit.toy.autoconfigure.ToyAutoConfiguration
- ToyAutoConfiguration文件
@Configuration注解阐明是Spring的配置类
@EnableConfigurationProperties(ToyProperties.class)注解指定了须要应用的属性配置类
package cn.bugkit.toy.autoconfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author bugkit
* @since 2022.2.24
*/
@Configuration
@EnableConfigurationProperties(ToyProperties.class)
public class ToyAutoConfiguration {
@Autowired
private ToyProperties toyProperties;
@Bean
public Toy toy(){
return new Toy(toyProperties.getName(),toyProperties.getPassword(), toyProperties.getWeight());
}
}
- ToyProperties文件
该类申明了须要从application.properties
文件读取的属性,供上述文件的toy()
办法构建所须要的Toy
对象。
/**
* @author bugkit
* @since 2022.2.24
*/
@Configuration
@ConfigurationProperties(prefix = "toy")
public class ToyProperties {
private String name;
private String password;
private int weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
- Toy文件
提供了咱们须要的showInfo()
办法,具体在toy-sample-app
我的项目中用到
public class Toy {
private String name;
private String password;
private int weight;
public Toy(String name, String password, int weight) {
this.name = name;
this.password = password;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void showInfo(){
System.out.println("===================start==========================");
System.out.println("Toy [ Name: " + name +", password: " + password +", weight: " + weight + " ]");
System.out.println("===================end============================");
}
}
toy-sample-app我的项目
该我的项目用于演示如何应用咱们本人构建的toy-spring-boot-starter
- pom文件
pom文件引入starter我的项目的jar
<dependency>
<groupId>cn.bugkit</groupId>
<artifactId>toy-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- ToyService文件
该类注入starter我的项目的Toy
bean,并在ToyService初始化实现后调用Toy
的showInfo()
办法
package cn.bugkit.toy.app.service;
import cn.bugkit.toy.autoconfigure.Toy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
/**
* @author bugkit
* @since 2022.2.24
*/
@Service
public class ToyService {
@Autowired
private Toy toy;
@PostConstruct
public void init() {
toy.showInfo();
}
}
- application.properties文件
该文件的key(如toy.name
)对应starter我的项目的ToyProperties
类。
toy.name=Hello Kitty
toy.password=kitty@1234
toy.weight=99
-
控制台启动日志
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.6.3) 2022-02-24 14:53:24.092 INFO 14652 --- [ main] c.bugkit.toy.app.StarterDemoApplication : Starting StarterDemoApplication using Java 1.8.0_211 on Bennetty74 with PID 14652 (E:\ideaProjects\spring-learning\toy-sample-app\target\classes started by Bennetty74 in E:\ideaProjects\spring-learning) 2022-02-24 14:53:24.092 INFO 14652 --- [ main] c.bugkit.toy.app.StarterDemoApplication : No active profile set, falling back to default profiles: default ===================start========================== Toy [ Name: Hello Kitty, password: kitty@1234, weight: 99 ] ===================end============================ 2022-02-24 14:53:24.608 INFO 14652 --- [ main] c.bugkit.toy.app.StarterDemoApplication : Started StarterDemoApplication in 0.888 seconds (JVM running for 1.836) Process finished with exit code 0
总结
toy-spring-boot-starter
: 咱们通过Spring Boot 的Auto-Configuration个性集成Toy
toy-sample-app
:在须要用到的starter的中央引入starter的maven依赖,从而能够应用Toy
对应的Spring Bean
可选
残余代码能够查看Github仓库源码,这里不再赘述。
代码地址:https://github.com/bennetty74…
发表回复