一、自定义starter
步骤如下:
- 创建maven项目,引入相关依赖
- 创建StudentProperties配置实体类 @ConfigurationProperties
- 编写StudentService功能类
- 创建StudentPropertiesAutoConfiguration自动配置类,启动StudentProperties类对象,创建StudentService对象。
- 编写META-INF/spring.factories,注册自定义的自动配置类。
1.创建项目
springboot官方starter起名:spring-boot-starter-xxx,非官方起名:xxx-spring-boot-starter
在此新建项目student-spring-boot-starter。
导入需要用到的依赖。
<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.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2.创建StudentProperties配置实体类
该配置类的属性即是yml配置文件的各项配置。
@ConfigurationProperties(prefix = "stu")表示会从配置文件中读取以 stu为前缀的属性,并将其映射到 StudentProperties 类的对应字段中。
@Data便捷创建getter,setter
@ConfigurationProperties(prefix = "stu")
@Data
public class StudentProperties {
private String name;
private Integer age;
}
3.编写功能代码
功能代码写入字段,构造器,以及方法myInfo。
public class StudentService {
private String name;
private Integer age;
public StudentService(String name, Integer age) {
this.name = name;
this.age = age;
}
public String myInfo(){
return "My name is "+name+",my age is "+age;
}
}
4.创建自动配置类
@Configuration声明该类是配置类,spring在启动时会处理这个类,注册相关的bean
@EnableConfigurationProperties(StudentProperties.class): 启用对 StudentProperties 类的配置属性支持。
@Configuration
@EnableConfigurationProperties(StudentProperties.class)
public class StudentPropertiesAutoConfiguration {
private StudentProperties studentProperties;
//通过构造函数注入StudentProperties。
//Spring Boot 在初始化时会自动从配置文件中读取属性,并通过构造函数注入到相应的对象中。
public StudentPropertiesAutoConfiguration(StudentProperties studentProperties) {
this.studentProperties = studentProperties;
}
@Bean
public StudentService studentService(){
return new StudentService(studentProperties.getName(),studentProperties.getAge());
}
}
5.编写META_INF/spring.factories
注册自定义的自动配置类StudentPropertiesAutoConfiguration。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.student.config.StudentPropertiesAutoConfiguration
至此,自定义starter已完成。为用于其他项目中,还需借助maven安装到本地仓库中。
clean
install
二、使用
1.创建springboot项目diy-test
引入相关依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
//导入自定义的starter
<dependency>
<groupId>com.student</groupId>
<artifactId>student-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2.编写controller,测试
@RestController
@RequestMapping("/test")
public class testController {
//注入之前编写的功能类
@Autowired
private StudentService studentService;
@GetMapping("/myInfo")
public String myInfo(){
//调用方法
return studentService.myInfo();
}
}
3.配置yaml文件
在配置文件中配置属性
server:
port: 8080
stu:
name: 小明
age: 18
4.启动项目
可以看到页面成功显示配置信息。
再次更改配置文件。
启动项目,成功显示。