自建 start

新建 start 项目

  1. 新建一个 spring 项目

    需要依赖 configuration 注解,直接加入了 springboot 的依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    <?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.4.0</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>start-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>start-demo</name>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>
    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    </plugin>
    </plugins>
    </build>

    </project>

  2. 新建一个需要被导入的类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class ImportClass {

    private String name;

    private Integer age;

    public ImportClass() {
    }
    public ImportClass(String name, Integer age) {
    this.name = name;
    this.age = age;
    }

    @Override
    public String toString() {
    return "ImportClass{" +
    "name='" + name + '\'' +
    ", age=" + age +
    '}';
    }
    }
  3. 新建一个 configuration,注入需要被导入的类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @EnableConfigurationProperties(ImportProperties.class)
    @Configuration
    public class StartConfiguration {

    @Bean
    public ImportClass instance() {
    return new ImportClass();
    }

    }
  4. 在 resource 目录下新建一个 META-INF 目录,增加一个 spring.factories 的文件

    1
    2
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.example.startdemo.configuration.StartConfiguration
  5. 打包 install

Springboot 项目引入 start

  1. 导入依赖

    1
    2
    3
    4
    5
    <dependency>
    <groupId>com.example</groupId>
    <artifactId>start-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
  2. 测试引入类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @SpringBootApplication
    public class SpringbootDemoApplication {

    public static void main(String[] args) {
    ConfigurableApplicationContext applicationContext = SpringApplication
    .run(SpringbootDemoApplication.class, args);

    ImportClass bean = applicationContext.getBean(ImportClass.class);
    System.out.println(bean.toString());
    }
    }

start 增加注入参数

  1. 增加依赖

    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>2.3.1.RELEASE</version>
    </dependency>
  2. 增加配置文件类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    @ConfigurationProperties(prefix = "start.config")
    public class ImportProperties {

    private String name = "bbb";

    private Integer age = 22;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Integer getAge() {
    return age;
    }
    public void setAge(Integer age) {
    this.age = age;
    }
    }
  3. 注入需要使用的类

    1
    2
    3
    4
    @Bean
    public ImportClass instance(ImportProperties importProperties) {
    return new ImportClass(importProperties.getName(),importProperties.getAge());
    }
  4. 打包 install

  5. springboot 项目使用,在 application.properties 中配置即可

    1
    2
    start.config.age= 33
    start.config.name= aaa

为注入参数增加注释

增加 additional-spring-configuration-metadata.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"properties": [
{
"name": "start.config.name",
"type": "java.lang.String",
"description": "config name",
"defaultValue": "zzz"
},
{
"name": "start.config.age",
"type": "java.lang.Integer",
"description": "config age",
"defaultValue": 18
}
]
}