- 创建Springboot项目
- 导入依赖
- 整体工程结构图
- 删除build
<?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> // start的基本信息 <groupId>com.xc</groupId> <artifactId>demo-start</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-start</name> <description>demo-start</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.6.13</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>// 配置文件的自动提示 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement></project>
- configgg删除主启动类配置
package com.xc.demostart;import com.xc.demostart.demo.test;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ConfigurationProperties(prefix = "xc.start")@Data@ComponentScanpublic class DemoStartConfig { private String accessKey; private String secretKey; @Bean public test xcApiClient() { return new test(accessKey, secretKey); }}
- 写下你想要实现的功能(这里我将简单地实现以下ak和sk返回我的配置)
package com.xc.demostart.demo;/** * @author xc * @date 2023/4/8 19:25 */public class test { private String ak; private String sk; public test(String ak,String sk) { this.ak = ak; this.sk = sk; } public String printAkSk() { return "ak: "+ak+"sk: "+sk; }}
在resources下新建包META-INF,Spring新配置文件.factories
# spring boot starterorg.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xc.demostart.DemoStartConfig
然后mvn install后到本地仓库进行测试
- 测试
随便创建一个maven项目
在pom.将刚刚的startttart引入xml
<dependency> <groupId>com.xc</groupId> <artifactId>demo-start</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
application.在yml中配置自己的ak,sk
xc: start: access-key: xcxc secret-key: 666
注意xc.start是刚刚在config配置文件中的prefix
@SpringBootTestclass TestStartApplicationTests { @Resource private DemoStartConfig demoStartConfig; @Test void contextLoads() { System.out.println(demoStartConfig.demoStart().printAkSk()); }}
控制台运行后输出
与我们直接写的配置相同,说明start开发成功