SpringBoot自定义starter
分析
自定义 starter
分为两个模块:
starter
启动器模块,空的jar
模块,pom
引入自动配置模块。别人引用starter
时只需引入starter
启动器模块即可- 自动配置模块
命名规范:
后缀:-spring-boot-starter
范例:mybatis-spring-boot-starter
自定义 starter
分析(自动配置模块):
- 我们的
starter
需要什么依赖 - 是否需要属性配置
- 配置类中需要提供哪些
Bean
交给Spring
容器管理 starter
加载是否依赖某些条件- 提供
META-INF/spring.factories
文件供@EnableAutoConfiguration
扫描
demo
maven
项目- 创建
mytest-spring-boot-starter
启动器模块,选择jar
- 创建
mytest-spring-boot-starter-autoconfigure
自动配置模块,选择jar
在
mytest-spring-boot-starter
的pom
中引入mytest-spring-boot-starter-autoconfigure
<dependencies>
<dependency>
<groupId>spring-boot</groupId>
<artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
配置
mytest-spring-boot-starter-autoconfigure
的pom文件<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
mytest-spring-boot-starter-autoconfigure
代码// 属性配置,这个配置在真正的项目中,需要在 application.yml 中配置 p7.hello 的配置项
@ConfigurationProperties(prefix = "p7.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
// 我们要让别人使用这个类
public class HelloService {
private HelloProperties helloProperties;
public String sayHello(String name) {
return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
// 配置类
@Configuration
// 在 Web 环境下才被加载
@ConditionalOnWebApplication
// 使配置属性立即生效
@EnableConfigurationProperties(value = HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
// 在 src/main/resources 下创建 META-INF 文件夹,再创建 spring.factories 文件,配置我们的配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.p7.autoconfigure.HelloServiceAutoConfiguration
创建
Springboot
的web
工程,在pom
中引入starter
启动器<dependency>
<groupId>spring-boot</groupId>
<artifactId>mytest-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在
application.yml
中配置starter
中需要的属性p7:
hello:
prefix: p7
suffix: hello
创建测试
Controller
@RestController("/hello")
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping
public String hello(){
return helloService.sayHello("say");
}
}
启动
web
项目,访问localhost:8080/hello
// web 启动类
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
还没有评论,来说两句吧...