有一些业务上需要在spring容器启动和关闭的时候做一些操作,那么可以自定义SmartLifecycle接口的实现类来扩展
比如RocketMq的接入
与@PostConstruct的区别:
@PostConstruct 是加在某个bean里的注解,是该bean实例化好后初始化之前即在initializeBean通过postProcessor(InitDestroyAnnotationBeanPostProcessor)调用
SmartLifecycle 和 lifeCyle 是接口,是当Spring容器加载所有bean并完成初始化之后(非懒加载),调用start方法
pom.xml配置依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.11.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-