赞
踩
一般情况下,我们会定义 2 个模块,其中一个负责自动装配,另一个负责依赖管理(启动器)
命名规范为:xxx-spring-boot-starter-autoconfigure、 xxx-spring-boot-starter
根据命名规范为: hello-springboot-starter-autoconfigure
- <?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.6.2</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example.starter</groupId>
- <artifactId>hello-springboot-starter-autoconfigure</artifactId>
- <version>0.0.1-SNAPSHOT</version>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <!--引入 spring-boot-starter,所有的 starter 都需要引入这个依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- </dependencies>
-
- </project>
这里我们定义一个 sayhi 的组件,支持自定义前后缀,目的是让使用者直接注入到容器中使用。
1.3.1 定义一个HelloService服务
- public class HelloService {
-
- private HelloProperties helloProperties;
-
- public HelloProperties getHelloProperties() {
- return helloProperties;
- }
-
- public void setHelloProperties(HelloProperties helloProperties) {
- this.helloProperties = helloProperties;
- }
-
- /**
- * say hi 方法
- * @param name
- * @return
- */
- public String sayHi(String name){
- return helloProperties.getPrefix() +"-"+ name +"-"+ helloProperties.getSuffix();
- }
- }
1.3.2 定义一个配置类,支持前后缀配置
- import org.springframework.boot.context.properties.ConfigurationProperties;
-
- @ConfigurationProperties(value = "example.hello")
- public class HelloProperties {
- private String prefix;
- private String suffix;
-
- public String getPrefix() {
- return prefix;
- }
-
- public void setPrefix(String prefix) {
- this.prefix = prefix;
- }
-
- public String getSuffix() {
- return suffix;
- }
-
- public void setSuffix(String suffix) {
- this.suffix = suffix;
- }
- }
1.3.3 定义自动配置类
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import com.example.starter.HelloProperties;
-
- @Configuration// 指定为配置类
- @ConditionalOnWebApplication // 指定条件:如果是 web 应用就生效
- @EnableConfigurationProperties( HelloProperties.class ) // 启动配置 properties
- public class HelloServiceAutoconfiguration {
-
- @Autowired
- private HelloProperties helloProperties;
-
- @Bean
- public HelloService helloService(){
- HelloService helloService = new HelloService();
- helloService.setHelloProperties(helloProperties);
- return helloService;
- }
- }
在 resources
目录下,新建 META-INF
目录,在目录新增一个名为 spring.factories
的配置文件,内容如下:
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.example.starter.HelloServiceAutoconfiguration
其中配置格式为 key,value 的格式,换行使用 \
, com.example.starter.HelloServiceAutoconfiguration
是我们自定义的自动配置类。
到此位置,自定义自动装配器就搞定了。接下来,我们写一下启动器
名为:hello-springboot-starter
当然我这里没有按照规范命名,最好命名为: hello-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>
-
- <!--自定义starter启动器-->
- <groupId>com.example.starter</groupId>
- <artifactId>hello-springboot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <dependencies>
- <!--引入自动装配模块-->
- <dependency>
- <groupId>com.example.starter</groupId>
- <artifactId>hello-springboot-starter-autoconfigure</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- </dependencies>
-
- </project>
好了,这个starter启动器不需要任何代码,只需要 pom 配置即可。
接下来,我们测试一下。
创建springboot项目,并引入自定义 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 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.6.2</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example.starter</groupId>
- <artifactId>hello-springboot-starter-test</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!--引入自定义 starter-->
- <dependency>
- <groupId>com.example.starter</groupId>
- <artifactId>hello-springboot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
当然这一步不是必要的,因为我们的案例支持自定义前后缀,所以要测试一下。
在 application.properties
配置文件配置我们定义的规则
- example.hello.prefix=Welcome
- example.hello.suffix=Hands up!!!
因为是自动装配,我们可以直接使用 @Autowired private HelloService helloService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class HelloController {
-
- @Autowired
- private HelloService helloService;
-
- @GetMapping("hello/{name}")
- public String hello(@PathVariable("name") String name){
- return helloService.sayHi(name);
- }
- }
项目启动后,访问浏览器 http://localhost:8080/hello/Jack
源代码见: https://gitee.com/yunnasheng/springboot-examples/tree/master/springboot-starter
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。