栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot

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...
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572466.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号