- 创建一个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>
- 删除主启动类配置config
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
@ComponentScan
public 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 starter
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xc.demostart.DemoStartConfig
然后mvn install后到本地仓库就可以测试用
- 测试
随便创建一个maven项目
在pom.xml中引入刚刚的start
<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
@SpringBootTest
class TestStartApplicationTests {
@Resource
private DemoStartConfig demoStartConfig;
@Test
void contextLoads() {
System.out.println(demoStartConfig.demoStart().printAkSk());
}
}
运行后控制台输出
与我们直接写的配置相同,说明start开发成功
标签:start,简易,xc,ak,boot,springframework,Start,开发,org From: https://blog.51cto.com/u_16123065/6454220