你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

SpringBoot_启动执行方式

2021/11/19 10:32:59

SpringBoot_启动执行方式

  • BeanPostProcessor
  • ServletContextListener
  • InitializingBean
  • PostConstruct
  • ApplicationRunner和CommandLineRunner
  • 执行顺序结果

BeanPostProcessor

Spring容器的创建Bean前后执行

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanPostProcessorTest implements BeanPostProcessor{
	

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.err.println("before init bean:"+beanName);
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.err.println("after init bean:"+beanName);
		return bean;
	}
}

ServletContextListener

ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件,该事件由ServletContextListener 来处理。

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletContextListenerTest implements ServletContextListener{

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.err.println("servletContextListener run...");
		
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.err.println("servletContextListener des run...");
	}

}

InitializingBean

InitializingBean接口为bean提供了属性初始化后的处理方法,在bean的属性初始化后都会执行该方法。

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class InitializingBeanTest implements InitializingBean{

	@Override
	public void afterPropertiesSet() throws Exception {
		System.err.println("initializingBean run...");

	}

}

PostConstruct

使用@PostConstruct注解一个方法来完成初始化,@PostConstruct注解的方法将会在依赖注入完成后被自动调用。

import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Configuration;

@Configuration
public class PostConstructTest {

	@PostConstruct
	public void init() {
		System.err.println("postConstruct run...");
	}
}

ApplicationRunner和CommandLineRunner

项目启动后执行,CommandLineRunner和ApplicationRunner的作用是相同的,不同在于参数的封装和没封装。可以创建多个实现CommandLineRunner和ApplicationRunner接口的类。为了使他们按一定顺序执行,可以使用@Order注解或实现Ordered接口。

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationRunnerTest implements ApplicationRunner{

	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.err.println("applicationRunner run...");
	}

}

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommandLineRunnerTest implements CommandLineRunner{

	@Override
	public void run(String... args) throws Exception {
		System.err.println("commandLineRunner run...");
	}

}

执行顺序结果

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

beanPostProcessor run...
servletContextListener run...
initializingBean run...
postConstruct run...
applicationRunner run...
commandLineRunner run...