springboot项目使用自定义starter
首先是自定义的starter部分
pom文件
<?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>
<groupId>com.common</groupId>
<artifactId>common-service</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
</parent>
<properties>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
代码部分
package com.common.starter;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2024/7/30 22:36
*/
public interface TestService {
void testPlay();
}
package com.common.starter;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2024/7/30 22:37
*/
public class TestServiceImpl implements TestService{
@Override
public void testPlay() {
System.out.println("自定义starter测试打印");
}
}
package com.common.starter.config;
import com.common.starter.TestService;
import com.common.starter.TestServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2024/7/30 22:35
*/
@Configuration
@ConditionalOnClass(TestService.class)
public class MyAutoConfiguration {
@Bean
public TestService testService(){
return new TestServiceImpl();
}
}
配置类部分

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.common.starter.config.MyAutoConfiguration
使用idea打包到本地maven仓库

使用部分

使用方完整pom
<?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>
<groupId>com.example</groupId>
<artifactId>consumer-service</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
</parent>
<dependencies>
<dependency>
<groupId>com.common</groupId>
<artifactId>common-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入junit单元测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
启动类
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2024/7/30 22:54
*/
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class,args);
}
}
测试类部分
package com.example;
import com.common.starter.TestService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @Description:
* @Author: tutu-qiuxie
* @Create: 2024/7/30 22:52
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Tests {
@Autowired
private TestService testService;
@Test
public void test002(){
testService.testPlay();
}
}

浙公网安备 33010602011771号