简单的SpringBootStarter
新建项目
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.13.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.13.RELEASE</version>
</dependency>
<!--springBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.13.RELEASE</version>
</dependency>
<!-- Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
创建配置类
@ConfigurationProperties("zhangfei.hello")
public class HelloProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
创建对外提供的接口
public class MyStartController {
HelloProperties helloProperties;
public MyStartController(HelloProperties helloProperties) {
this.helloProperties=helloProperties;
}
public String index(){
return helloProperties.getName()+"欢迎您";
}
}
启动类
启动类 首先看引用项目的配置 是否有zhangfei.hello.name 这个属性 有的话 加载HelloProperties.class 然后开始注入bean信息(MyStartController ) 外部引入jar包即可用
@Configuration
@ConditionalOnProperty(value = "zhangfei.hello.name")
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfitguration {
@Autowired
HelloProperties helloProperties;
@Bean
public MyStartController indexController(){
return new MyStartController(helloProperties);
}
}
启动类向导
这个一定注意
resources下创建META-INF 在META-INF下创建文件spring.factories 而不是在创建再创建spring文件夹然后再创建 如果factories文件 如果META-INF下面创建spring的话 再spring文件夹创建org.springframework.boot.autoconfigure.AutoConfiguration.imports是可以的
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gouying.web.config.HelloAutoConfitguration