spring-boot创建starter项目
2021-03-11 本文已影响0人
TZX_0710
创建maven项目
引入编写starter所需要的spring依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.12.RELEASE</version>
</dependency>
- 编写Properties类、主要用于接受spring-boot启动时候application.properties/yml文件的配置参数
package com.enna.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author DELL
* @version 1.0
* @date 2021/3/11 16:29
*/
@Component
@ConfigurationProperties(prefix = "test")
public class PropertyDemo {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
- 编写AutoConfiguration配置文件
package com.enna.test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* TODO
*
* @author DELL
* @version 1.0
* @date 2021/3/11 16:31
*/
@Configuration
@EnableConfigurationProperties(TestProperty.class)
public class TestAutoConfiguration {
//currentUsers为自定义测试新建的对象
@Bean
public CurrentUsers users(TestProperty testProperty) {
TestUsers testUsers = new TestUsers();
System.out.println( "注入testUsers" );
System.out.println( testUsers );
return testUsers;
}
}
在resource目录下面创建META-INF/spring.factories文件
让spring-boot能够扫描到autoconfiguration类
至此starter简易demo编写完成,采用maven的package打包 然后丢入所需要的starter项目
在所需要的项目里面引入、配置applicationProperties