Springboot-自定义starter
starter简介
-
SpringBoot starter机制
SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。 -
为什么要自定义starter
在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,SpringBoot为我们完成自动装配,简直不要太爽。 -
自定义starter的案例
▲ 动态数据源。
▲ 登录模块。
▲ 基于AOP技术实现日志切面。
。。。。。。
- 自定义starter的命名规则
SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。也可以不接受建议。
新建一个springboot项目springboot-mystarter
1. pom.xml
<?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 https://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>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-mystarter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mystarter</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
2.HelloService
package cn.demo.starter;
/**
* 描述:随便定义一个Service
**/
public class HelloService {
private String sayWhat;
private String toWho;
public HelloService(String sayWhat, String toWho){
this.sayWhat = sayWhat;
this.toWho = toWho;
}
public String say(){
return this.sayWhat + "! " + toWho;
}
}
3. HelloProperties
package cn.demo.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 描述:配置信息 实体
**/
@ConfigurationProperties(prefix = "demo")
public class HelloProperties {
private String sayWhat;
private String toWho;
public String getSayWhat() {
return sayWhat;
}
public void setSayWhat(String sayWhat) {
this.sayWhat = sayWhat;
}
public String getToWho() {
return toWho;
}
public void setToWho(String toWho) {
this.toWho = toWho;
}
}
4. HelloConfig
自动装配类
package cn.demo.starter;
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(HelloProperties.class)
@ConditionalOnProperty(prefix = "demo", name = "isopen", havingValue = "true")
public class HelloConfig {
@Autowired
private HelloProperties helloProperties;
@Bean(name = "demo")
public HelloService demoService() {
return new HelloService(helloProperties.getSayWhat(), helloProperties.getToWho());
}
}
5. 其他类
测试@Service注解的类,打包后能否自动注入
package cn.demo.starter;
/**
* @Author ccl
* @Date 2021/4/12 11:31
*/
public interface UserSerive {
void deleteAllUsers();
}
package cn.demo.starter;
import org.springframework.stereotype.Service;
/**
* @Author ccl
* @Date 2021/4/12 11:32
*/
@Service
public class UserServiceImpl implements UserSerive{
@Override
public void deleteAllUsers() {
System.out.println("删除所有用户");
}
}
6. spring.factories
在resources下新建目录META-INF,META-INF下新建文件spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.demo.starter.HelloConfig
7. 去掉多余文件
去掉启动类;
去掉application.properties,当然也可以保留作为外层配置参考。
最终目录结构如图:
8.打包
执行命令 mvn clean install
将自定义starter打成jar包,并发布到本地仓库。
使用starter
再新建一个springboot项目springboot-depend-mystarter
1. pom.xml
<?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 https://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>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-depend-mystarter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-depend-mystarter</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-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>springboot-mystarter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2. application.properties
这里配置上自定义starter需要使用的参数
demo.isopen=true
demo.to-who=Tom
demo.say-what=are you ok?
3. Application.java
最好指定包扫描路径
package cn.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"cn"})
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4. TestController.java
测试调用自定义starter里面的接口是否正常
package cn.demo;
import cn.demo.starter.HelloService;
import cn.demo.starter.UserSerive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
/**
* @Author ccl
* @Date 2021/4/11 21:50
*/
@RestController
public class TestController {
@Autowired
private HelloService helloService;
@Autowired
private UserSerive userSerive;
@RequestMapping("/test1")
public Object test1() {
String say = helloService.say();
System.out.println(say);
userSerive.deleteAllUsers();
return say;
}
}
5. 结果打印
are you ok?! Tom
删除所有用户