springboot框架学习积累—SpringBoot自定义starter
1. SpringBoot的starter机制
2. 自定义starter 的原因
3. 自定义starter的命名规范
4. 自定义starter的步骤
-
新建
maven jar
工程,工程名是zdy-spring-boot-starter
,导入自动配置的依赖<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.2.2.RELEASE</version> </dependency> </dependencies>
-
编写
javaBean
@EnableConfigurationProperties(SimpleBean.class) //开启@ConfigurationProperties注解 @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
,让springboot
完成自动配置的配置类@Configuration @ConditionalOnClass //当类路径classpath下有指定的类的情况下,就会进行自动装配 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.lagou.config.MyAutoConfiguration #类路径