Springboot-自建start
自建 start
新建 start 项目
新建一个 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
<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>新建一个需要被导入的类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class ImportClass {
private String name;
private Integer age;
public ImportClass() {
}
public ImportClass(String name, Integer age) {
this.name = name;
this.age = age;
}
public String toString() {
return "ImportClass{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}新建一个 configuration,注入需要被导入的类
1
2
3
4
5
6
7
8
9
10
public class StartConfiguration {
public ImportClass instance() {
return new ImportClass();
}
}在 resource 目录下新建一个 META-INF 目录,增加一个 spring.factories 的文件
1
2org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.startdemo.configuration.StartConfiguration打包 install
Springboot 项目引入 start
导入依赖
1
2
3
4
5<dependency>
<groupId>com.example</groupId>
<artifactId>start-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>测试引入类
1
2
3
4
5
6
7
8
9
10
11
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
2
3
4
5<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>增加配置文件类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}
}注入需要使用的类
1
2
3
4
public ImportClass instance(ImportProperties importProperties) {
return new ImportClass(importProperties.getName(),importProperties.getAge());
}打包 install
springboot 项目使用,在 application.properties 中配置即可
1
233 =
aaa =
为注入参数增加注释
增加 additional-spring-configuration-metadata.json
1 | { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 zeofuns!