springboot项目使用自定义starter
首先是自定义的starter部分
pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?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> |
代码部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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测试打印" ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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(); } } |
配置类部分
1 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.common.starter.config.MyAutoConfiguration |
使用idea打包到本地maven仓库
使用部分
使用方完整pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?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> |
启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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); } } |
测试类部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异