-
导入springboot自动配置的相关jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
-
创建所要导入的bean对象,用@ConfigurationProperties和@EnableConfigurationProperties标记
@EnableConfigurationProperties(SimpleBean.class)
@ConfigurationProperties(prefix = "simplebean")
public class SimpleBean {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "SimpleBean{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
-
创建自动配置类MyAutoConfiguration,使用@Bean进行自动导入
@Configuration
public class MyAutoConfiguration {
static {
System.out.println("MyAutoConfiguration init...");
}
@Bean
public SimpleBean simpleBean(){
return new SimpleBean();
}
}
-
在resources下创建META-INF/spring.factories,编辑所需扫描的自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lb.config.MyAutoConfiguration