项目层级
1.引入依赖
<?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>
<groupId>com.example</groupId>
<artifactId>define-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>define-starter</name>
<description>自定义starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
</project>
2.定义需要注入的Bean以及测试方法
/**
* @Author: Zoups
* @Date: 2023/01/16/16:02
* @Description: 需要注册的Bean
*/
public class GetMoneyService {
/**
* 自定义一个方法
* @return
*/
public String getMoneyUtil(){
return "我想变有钱";
}
}
3.定义配置类
import com.example.definestarter.service.GetMoneyService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: Zoups
* @Date: 2023/01/16/16:01
* @Description: starter配置类
*/
@Configuration
public class StarterConfig {
@Bean
public GetMoneyService getMoneyService(){
return new GetMoneyService();
}
}
4.resources下定义配置文件spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.definestarter.config.StarterConfig
5.install 把自定义starter打到本地maven仓库
可以在本地maven仓库找到
路径为: IDEA设置的maven仓库路径+pom里面定义的包路径
6.引入starter依赖测试
任意创建一个项目引入自定义的starter依赖
<dependency>
<groupId>com.example</groupId>
<artifactId>define-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
测试类调用starter里的方法
可以看到返回了starter里面的getMoneyUtil方法的结果