SpringBoot自定义starter
SpringBoot自定义starter
1. 新建maven项目导入pom文件
以下是基本的依赖,如果用到其它东西,还可以导入其它依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.0</version>
<!--依赖不会被传递-->
<optional>true</optional>
</dependency>
<!--注解配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
2. 映射配置文件类
package com.shaoby.mytest;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @Author
* @Date 2024/5/26 21:26
*/
@Component
@ConfigurationProperties(prefix = "shaoby.my-test")
@PropertySource("classpath:application.properties")
public class MyTestProperties {
private String name = "MyTest";
private String host = "localhost";
private String port = "8080";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
3.配置类
package com.shaoby.mytest;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author
* @Date 2024/5/26 21:28
*/
@EnableConfigurationProperties(MyTestProperties.class)
@Configuration
public class MyTestConfig {
private MyTestProperties properties;
public MyTestConfig (MyTestProperties properties){
this.properties = properties;
}
/**
* 注入Test类的Bean对象到IOC容器
* @return
*/
@Bean
public Test test(){
return new Test(properties.getName(), properties.getHost(),properties.getPort());
}
}
4.将配置类暴漏给SpringBoot
在MATE-INF目录下创建spring.factories文件,注意换行要加斜杠
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.shaoby.mytest.MyTestConfig
5.配置类提示
在MATE-INF目录下创建文件additional-spring-configuration-metadata.json,这样在其它项目导入自定义starter后,编写配置文件时可以起到提示作用。
{
"properties": [{
"name": "shaoby.my-test.name",
"type": "java.lang.String",
"description": "这是MyTest的name.",
"defaultValue": "MyTest"
},{
"name": "shaoby.my-test.host",
"type": "java.lang.String",
"description": "这是MyTest的host.",
"defaultValue": "localhost"
},
{
"name": "shaoby.my-test.port",
"type": "java.lang.String",
"description": "这是MyTest的port.",
"defaultValue": "8080"
}]
}
6.打包,引入SpringBoot项目测试
引入starter坐标
<dependency>
<groupId>org.mytest</groupId>
<artifactId>test-spring-boot-start</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
可以配置配置文件
shaoby.my-test.name=hello222
测试
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// SpringApplication.run(Application.class, args);
AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(Application.class);
Test test = c.getBean(Test.class);
test.test();
}
}
结果,bean对象已经注入到容器中。
自动装配成功----
name->hello222
host->localhost
port->8080
问题
我的配置文件类使用了@Component和@PropertySource(“classpath:application.properties”)注解,不然引入到springboot项目里后读取不到配置文件,不知道为啥????