参考资料:
具体过程:
- 项目结构
hello-spring-boot-starter
│
├── pom.xml
│
├── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── hellostarterdemo
│ │ ├── HelloProperties.java
│ │ ├── HelloService.java
│ │ └── HelloAutoConfiguration.java
│ │
│ └── resources
│ └── META-INF
│ └── spring.factories
- pom.xml
XML
<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.7.18</version>
</parent>
<groupId>com.example</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Starter核心依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 自动生成配置提示(可选) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
- HelloProperties
java
package com.example.hellostarterdemo;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
/**
* 默认值
*/
private String name = "World";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- HelloService
java
package com.example.hellostarterdemo;
public class HelloService {
private final HelloProperties properties;
public HelloService(HelloProperties properties) {
this.properties = properties;
}
public String sayHello() {
return "Hello " + properties.getName();
}
}
- HelloAutoConfiguration
java
package com.example.hellostarterdemo;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public HelloService helloService(HelloProperties properties) {
return new HelloService(properties);
}
}
- spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.hellostarterdemo.HelloAutoConfiguration
- 然后执行命令
bash
mvn clean install
使用过程:
- 添加依赖
XML
<dependency>
<groupId>com.example</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
- application.yml
python
hello:
name: GPT
- Controller
java
package com.example.demo.controller;
import com.example.hellostarterdemo.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("starter")
public class StarterController {
@Autowired
private HelloService helloService;
@GetMapping("hello")
public String hello() {
return helloService.sayHello();
}
}