自定义start
SpringBoot核心是maven依赖管理和start,我们自定义的start可以引入第三方依赖,并且提供自定义bean对象交给Spring容器管理。其他项目直接引入自定义start,就可以省去引入第三方依赖和手动创建bean的繁琐操作。
如何区分自定义start和官方start
官方start的命名规范是spring-boot-starter-**
比如下面的这个web依赖,而自定义start名字一般是**-spring-boot-start
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
|
自定义start的原理
SpringBoot在启动的时候会扫描所有依赖或者说jar包中resources/META-INF/spring.factories文件,并会将该文件中配置的类注入到IOC容器中
步骤
1.在自定义的start项目中声明一个配置类,在该配置类中配置一个bean
2.将该配置类通过pringframework.boot.autoconfigure.EnableAutoConfiguration配置到spring.factories文件中即可
自定义start功能描述及实现
需求
现在需要自定义个start,功能是提供一个Person对象。在其他Springboot模块中引入该start,并且获取该bean对象
1.创建一个配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.example.config;
import com.example.Cat; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class CatAutoConfiguration {
@Bean public Cat cat(){ return new Cat("tom",12); } }
|
2.添加resources/META-INF/spring.factories配置
1
| org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.config.CatAutoConfiguration
|
代码升级
升级原因
cat()方法中返回的对象的属性是写死的,最好改成可配置的
升级步骤
1.在Cat类上添加@ConfigurationProperties(prefix = “cat”)注解,引用配置文件中的数据,代码如下
1 2 3 4 5 6 7 8 9
| import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.Objects;
@ConfigurationProperties(prefix = "cat") public class Cat { private String name; private Integer age; }
|
2.在配置类中使用EnableConfigurationProperties引入Cat配置文件,并将cat注入到cat方法中代码如下
1 2 3 4 5 6 7 8 9
| @Configuration @EnableConfigurationProperties(Cat.class) public class CatAutoConfiguration {
@Bean public Cat cat(Cat cat){ return new Cat(cat.getName(),cat.getAge()); } }
|
注意事项:此种方式只有配置文件在SpringBoot项目才生效,在当前start项目的配置文件中不生效
注意事项
start项目的pom中不能有build标签,否则引入该start的springboot项目不能正常打包
事件监听
springboot提供的监听器如下
- ApplicationContextInitializer、
- SpringApplicationRunListener、
- CommandLineRunner、
- ApplicationRunner
当springboot启动的时候会自动执行监听器中的方法。
其中CommandLineRunner、ApplicationRunner通过加@Component即可直接剩下。剩下的两个需要配置到spring.factories文件中才能生效
SpringBoot监控-actuator基本使用
使用步骤
第一步导入依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
第二步访问http://localhost:8080/acruator页面查看信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| { "_links":{ "self":{ "href":"http://localhost:8080/actuator", "templated":false }, "health":{ "href":"http://localhost:8080/actuator/health", "templated":false }, "health-component-instance":{ "href":"http://localhost:8080/actuator/health/{component}/{instance}", "templated":true }, "health-component":{ "href":"http://localhost:8080/actuator/health/{component}", "templated":true }, "info":{ "href":"http://localhost:8080/actuator/info", "templated":false } } }
|
通过网址查看配置信息,比如配置信息如下
1 2
| info.name=lucy info.age=99
|
查看地址http://localhost:8080/actuator/info ,其中info表示配置文件的前缀
配置信息
开启健康检查详细信息
1
| management.endpoint.health.show-details=always
|
开启所有endpoint
1
| management.endpoints.web.exposure.include=*
|
SpringBoot admin图形化工具
因为actuator查看数据不方便,所以使用admin
使用步骤
第一步创建server
1 2 3 4
| <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency>
|
在启动类上添加@EnableAdminServer
第二步创建client
1 2 3 4
| <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency>
|
spring.boot.admin.client.url=http://localhost:9000
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
第三步
启动server和client,访问server即可
SpringBoot项目部署
指定打包名称
1 2 3 4 5 6 7 8 9
| <build> <finalName>springboot</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
方式1
直接打jar包,运行jar包即可 java -jar xxx.jar
方式2
打war包,发布到tomcat中,打包前需要修改启动类,让启动类继承SpringBootServletInitializer,并重写configure方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @SpringBootApplication public class SpringbootDeployApplication extends SpringBootServletInitializer {
public static void main(String[] args) { SpringApplication.run(SpringbootDeployApplication.class, args); }
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(SpringbootDeployApplication.class); } }
|