背景
都说不要造轮子,但是青柠认为,有的时候,造个轮子,更有助于清楚详细的业务逻辑,所以咱也自己写一个轮子,搞个系统开发。大体思路就是先搭建基础框架,然后细写业务逻辑,会涵盖主流的中间件,数据库,框架等。
一、新建一个SpringBoot项目my-spring-boot-starter
项目目录结构如下:
二、项目依赖文件
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.7</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.qnbc</groupId><artifactId>qnbc-boot-start</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
三、resources目录下添加目录META-INF下的文件spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.example.myspringbootstarter.MyServiceAutoConfiguration
读者可以查看SpringBoot自动配置原理,即可明白此处配置。此处MyServiceAutoConfiguration类为配置类,并将MyService交给Spring管理。
四、编写代码配置类和测试接口
public class MyService {public String hello(String name){return "Hello "+ name + " !";
}
}@Configuration
@ConditionalOnWebApplication
public class MyServiceAutoConfiguration {
@Bean
public MyService hello(){return new MyService();
}
}
五、新建测试工程,引入自定义starter的依赖
<?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.qnbc.test</groupId><artifactId>qnbc-boot-test</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.qnbc</groupId><artifactId>qnbc-boot-start</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>
此处引入自定义的starter依赖(使用Maven工具install原starter项目):
<dependency><groupId>com.example</groupId><artifactId>my-spring-boot-starter</artifactId><version>0.0.1-SNAPSHOT</version></dependency>
说明:引入自定义starter后,在项目的依赖下可以查看到:
六、编写测试类
@RestController
public class TestController {@Autowired
private MyService myService;@GetMapping("/hello")
public String hello(@RequestParam(value = "name") String name){return myService.hello(name);
}
}
七、启动测试项目并测试
至此,封装自己的SpringBoot starter成功!
总结
以上就是本篇文章的全部内容,希望能对大家的学习有所帮助,评论区留下“管用",记得三联哦。
还有其他知识分享,欢迎拜访 https://blog.csdn.net/weixin_40379712?spm=1011.2124.3001.5343