springboot的强大之处在于把我们常用的场景抽取成一个个stater(场景启动器),我们通过引入springboot为我们提供的这些场景启动器,我们在进行少量的配置就能使用相应的功能。即使是这样,springboot也不可能囊括我们所有的使用场景,往往我们就需要自定义stater,来简化我们对springboot的使用。
我们在使用 springboot admin server的时候,通常做一下两个步骤:
1、添加依赖
2、开启注解
<!-- SpringBoot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- SpringBoot Admin -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
我们看下这个依赖包
spring-boot-admin-server这个pom.xml 第一个依赖就是
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.7.RELEASE</version>
<scope>compile</scope>
</dependency>
spring-boot-starter 是自定义启动器必须的
spring-boot-admin-stater-server-2.2.4.jar只是一个pom,并无实际逻辑,继续看spring-boot-admin-server这个依赖,这是spring-bootadmin-stater-server启动器的配置模块
SPI自动注入了4个配置,打开一个看看AdminServerAutoConfiguration
在启动类上添加开启服务的注解
@EnableAdminServer @SpringBootApplication public class MonitorApplication { public static void main(String[] args) { SpringApplication.run(MonitorApplication.class, args); } }我们详细看下这个注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented // 导入admin服务的标记配置,用于标识admin服务的启用,在后面服务实例化时作为依赖项引入 @Import(AdminServerMarkerConfiguration.class) public @interface EnableAdminServer { } @Configuration(proxyBeanMethods = false) public class AdminServerMarkerConfiguration { // 向spring容器添加bean组件 @Bean public Marker adminServerMarker() { return new Marker(); } public static class Marker { } }
guo-hello-spring-boot-starter 场景启动器
guo-hello-spring-boot-starter-autoconfigurer 启动器配置模块
hello-spring-boot-starter-test 自定义启动器测试模块
guo-hello-spring-boot-starter 添加启动器配置模块
<dependency>
<groupId>com.guo</groupId>
<artifactId>
guo-hello-spring-boot-starter-autoconfigurer
</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
guo-hello-spring-boot-starter-autoconfigurer添加核心启动器依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(GuoHelloServerMarkerConfiguration.class)
public @interface EnableGuoHelloService {
}
@Configuration(proxyBeanMethods = false)
public class GuoHelloServerMarkerConfiguration {
@Bean
public Marker guoHelloServerMarker() {
return new Marker();
}
public static class Marker {
}
}
在资源目录下创建目录 META-INF,在目录下创建文件spring.factories,内容如下
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.guo.stater.config.GuoHelloServiceAutoConfiguration
@Configuration(proxyBeanMethods = false)
// 启用注解时EnableGuoHelloService注入bean GuoHelloServerMarkerConfiguration.Marker
@ConditionalOnBean(GuoHelloServerMarkerConfiguration.Marker.class)
// 服务依赖的配置
@EnableConfigurationProperties(GuoHelloServerProperties.class)
// 导入其他服务配置
@ImportAutoConfiguration({ GuoHelloServerConfiguration.class })
@AutoConfigureAfter({ WebClientAutoConfiguration.class })
@Lazy(false)
public class GuoHelloServiceAutoConfiguration {
}
@lombok.Data
@Configuration(proxyBeanMethods = false)
public class GuoHelloServerConfiguration {
@Autowired
private GuoHelloServerProperties guoHelloServerProperties;
public String sayHi(String name) {
return name + " " + guoHelloServerProperties.getHi();
}
}
@lombok.Data
@ConfigurationProperties("spring.boot.guo.hello")
public class GuoHelloServerProperties {
private String hi;
}
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //指定条件成立的情况下自动配置类生效
@AutoConfigureOrder //指定自动配置类的顺序
@Bean //向容器中添加组件
@ConfigurationProperties //结合相关xxxProperties来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
自动配置类要能加载需要将自动配置类,配置在META-INF/spring.factories中
<!-- 引入自定义starter -->
<dependency>
<groupId>com.guo</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
// 添加注解开启服务
@EnableGuoHelloService
@SpringBootApplication
public class StaterApplication {
public static void main(String[] args) {
SpringApplication.run(StaterApplication.class, args);
}
}
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@Autowired
private GuoHelloServerConfiguration guoHelloServerConfiguration;
@GetMapping("/hello/{name}")
public String hello(@PathVariable(value = "name") String name) {
return helloService.sayHello( name + " , " );
}
@GetMapping("/hi/{name}")
public String hi(@PathVariable(value = "name") String name) {
return guoHelloServerConfiguration.sayHi(name);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。