当前位置: 首页 > news >正文

贵州省建设厅公示网站长春关键词搜索排名

贵州省建设厅公示网站,长春关键词搜索排名,国内最好的在线网站建设,fw可以做网站文章目录 前言SpringBoot核心源码拓展Initializer拓展监听器ApplicationListenerBeanFactory的后置处理器 & Bean的后置处理器AOP其他的拓展点 前言 当我们引入注册中心的依赖,比如nacos的时候,当我们启动springboot,这个服务就会根据配置…

文章目录

  • 前言
  • SpringBoot核心源码
  • 拓展Initializer
  • 拓展监听器ApplicationListener
  • BeanFactory的后置处理器 & Bean的后置处理器
  • AOP
  • 其他的拓展点

前言

  • 当我们引入注册中心的依赖,比如nacos的时候,当我们启动springboot,这个服务就会根据配置文件自动注册到注册中心中,这个动作是如何完成的?
  • 注册中心使用了SpringBoot中的事件监听机制,在springboot初始化的时候完成服务注册

SpringBoot核心源码

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {  ...this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));// Servletthis.webApplicationType = WebApplicationType.deduceFromClasspath();  this.bootstrapRegistryInitializers = new ArrayList(this.getSpringFactoriesInstances(BootstrapRegistryInitializer.class));  // 注意这里,Initializersthis.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));  // 注意这里 Listenersthis.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));  this.mainApplicationClass = this.deduceMainApplicationClass();  
}

我们可以看到空的SpringBoot项目有一些initializers以及一些listeners
在这里插入图片描述
在这里插入图片描述

注意这两行,换言之我们只要实现这两个类就可以自定义拓展SpringBoot了!
在这里插入图片描述

这里和手写Starter都是对SpringBoot的拓展,有兴趣的小伙伴可以看这篇文章

拓展Initializer

再看这张图
在这里插入图片描述

我们需要研究一下ApplicationContextInitializer这个类:

@FunctionalInterface  
public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {  /**  * Initialize the given application context.  * @param applicationContext the application to configure  */  void initialize(C applicationContext);  
}

这样就很清晰了,我们尝试手写一个继承类:

public class DemoInitializer implements ApplicationContextInitializer {  @Override  public void initialize(ConfigurableApplicationContext applicationContext) {  System.out.println("自定义初始化器执行...");  ConfigurableEnvironment environment =  applicationContext.getEnvironment();  Map<String, Object> map = new HashMap<>(1);  map.put("name", "sccccc");  environment.getPropertySources().addLast(new  MapPropertySource("DemoInitializer", map));  System.out.println("DemoInitializer execute, and add some property");  }  
}

通过SPI机制将自定义初始化器交给list集合initializers
在这里插入图片描述

然后再debug,就会发现:
在这里插入图片描述

最后经过一次回调:

private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,  ...  applyInitializers(context);  ...// Add boot specific singleton beans 下面是beanFactory的操作

遍历所有的初始化器,然后

/**  
* Apply any {@link ApplicationContextInitializer}s to the context before it is  
* refreshed.  
* @param context the configured ApplicationContext (not refreshed yet)  
* @see ConfigurableApplicationContext#refresh()  
*/  
@SuppressWarnings({ "rawtypes", "unchecked" })  
protected void applyInitializers(ConfigurableApplicationContext context) {  for (ApplicationContextInitializer initializer : getInitializers()) {  Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),  ApplicationContextInitializer.class);  Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");  initializer.initialize(context);  }  
}

在这里插入图片描述

流程:
在这里插入图片描述

拓展监听器ApplicationListener

在这里插入图片描述

@FunctionalInterface  
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {  /**  * Handle an application event.  */  void onApplicationEvent(E event);  /**  * Create a new {@code ApplicationListener} for the given payload consumer.  */  static <T> ApplicationListener<PayloadApplicationEvent<T>> forPayload(Consumer<T> consumer) {  return event -> consumer.accept(event.getPayload());  }  }

这里和上面initializer一样,就不演示了

BeanFactory的后置处理器 & Bean的后置处理器

在这里插入图片描述

Spring Boot解析配置成BeanDefinition的操作在invokeBeanFactoryPostProcessors方法中
自定义BeanFactory的后置处理器:

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactorybeanFactory) throws BeansException {Arrays.asList(beanFactory.getBeanDefinitionNames()).forEach(beanDefinitionName ->System.out.println(beanDefinitionName));System.out.println("BeanFactoryPostProcessor...");}
}

自定义Bean的后置处理器:

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {if(beanName.equals("userController")){System.out.println("找到了userController: "+bean);}return null;}
}

AOP

这个相信大家用的比较多,可以自定义切面:

@Aspect
@Component
public class LogAspect {// 切入点 Pointcut   可以对Service服务做切面
@Pointcut("execution(* com.example.service.*.*(..))")
public void mypointcut(){}// 前置通知
@Before(value = "mypointcut()")
public void before(JoinPoint joinPoint){System.out.println("[前置通知] 准备开始记录日志...");System.out.println("[前置通知] 目标类是: "+joinPoint.getTarget());System.out.println("[前置通知] 目标方法是:"+joinPoint.getSignature().getName());
}// 后置通知
@AfterReturning(value = "mypointcut()")
public void afterReturning(JoinPoint joinPoint){System.out.println("[后置通知] 记录日志完成...");System.out.println("[后置通知] 目标类是: "+joinPoint.getTarget());System.out.println("[后置通知] 目标方法是:"+joinPoint.getSignature().getName());
}/*@Around(value = "mypointcut()")
public void around(ProceedingJoinPoint joinPoint){System.out.println("[环绕通知] 日志记录前的操作...");try {joinPoint.proceed();System.out.println("[环绕通知] 日志记录后的操作...");System.out.println("[环绕通知] "+joinPoint.getTarget());System.out.println("[环绕通知] "+joinPoint.getSignature().getName());} catch (Throwable throwable) {System.out.println("[环绕通知] 发生异常的操作...");throwable.printStackTrace();}finally {...}
}

其他的拓展点

  1. Banner

方法地址:
printBanner(env)->bannerPrinter.print->SpringBootBanner#printBanner
可以在resource目录下建立banner.txt文件夹实现自定义Banner

  1. Runners

流程:
在这里插入图片描述

自定义:

@Component
public class JackApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("JackApplicationRunner...");}
}

文章转载自:
http://piperine.tbjb.cn
http://underlet.tbjb.cn
http://carposporangium.tbjb.cn
http://nuttiness.tbjb.cn
http://solidly.tbjb.cn
http://accessory.tbjb.cn
http://culmiferous.tbjb.cn
http://forecastleman.tbjb.cn
http://chiroptera.tbjb.cn
http://enfeoff.tbjb.cn
http://ruskinize.tbjb.cn
http://rooter.tbjb.cn
http://surfy.tbjb.cn
http://ibizan.tbjb.cn
http://emitter.tbjb.cn
http://intermittence.tbjb.cn
http://athwartship.tbjb.cn
http://munitions.tbjb.cn
http://secreta.tbjb.cn
http://puggree.tbjb.cn
http://metapolitics.tbjb.cn
http://extraversion.tbjb.cn
http://vacua.tbjb.cn
http://strew.tbjb.cn
http://popskull.tbjb.cn
http://unicursal.tbjb.cn
http://rooseveltiana.tbjb.cn
http://immunopathology.tbjb.cn
http://grouch.tbjb.cn
http://auxiliary.tbjb.cn
http://tensibility.tbjb.cn
http://martyrologist.tbjb.cn
http://amniography.tbjb.cn
http://corruptionist.tbjb.cn
http://diplosis.tbjb.cn
http://miacid.tbjb.cn
http://circumjacent.tbjb.cn
http://noddy.tbjb.cn
http://aeroshell.tbjb.cn
http://wellborn.tbjb.cn
http://reinstitution.tbjb.cn
http://lespedeza.tbjb.cn
http://reverie.tbjb.cn
http://sesame.tbjb.cn
http://chthonian.tbjb.cn
http://shooter.tbjb.cn
http://abscondence.tbjb.cn
http://gilgamesh.tbjb.cn
http://misclassify.tbjb.cn
http://lactogenic.tbjb.cn
http://upbraiding.tbjb.cn
http://morganite.tbjb.cn
http://sampler.tbjb.cn
http://acrophony.tbjb.cn
http://hamburger.tbjb.cn
http://field.tbjb.cn
http://allemande.tbjb.cn
http://sinopis.tbjb.cn
http://travertin.tbjb.cn
http://gyronny.tbjb.cn
http://peitaiho.tbjb.cn
http://intergovernmental.tbjb.cn
http://furred.tbjb.cn
http://baronage.tbjb.cn
http://pittsburgh.tbjb.cn
http://bodoni.tbjb.cn
http://religieux.tbjb.cn
http://saltchucker.tbjb.cn
http://angelino.tbjb.cn
http://conflictive.tbjb.cn
http://electromotor.tbjb.cn
http://poplin.tbjb.cn
http://triglyph.tbjb.cn
http://rod.tbjb.cn
http://frow.tbjb.cn
http://delict.tbjb.cn
http://semiglazed.tbjb.cn
http://sucrose.tbjb.cn
http://saponification.tbjb.cn
http://company.tbjb.cn
http://sardonic.tbjb.cn
http://aquifer.tbjb.cn
http://phytoplankton.tbjb.cn
http://hibernicism.tbjb.cn
http://mukluk.tbjb.cn
http://despiteously.tbjb.cn
http://hematose.tbjb.cn
http://irreverently.tbjb.cn
http://impark.tbjb.cn
http://bird.tbjb.cn
http://acushla.tbjb.cn
http://perchromate.tbjb.cn
http://deserter.tbjb.cn
http://apollonian.tbjb.cn
http://okey.tbjb.cn
http://recremental.tbjb.cn
http://shasta.tbjb.cn
http://bushie.tbjb.cn
http://aleppo.tbjb.cn
http://radiosterilize.tbjb.cn
http://www.dt0577.cn/news/93270.html

相关文章:

  • 宁波建站价格合肥网络营销公司
  • 云虚拟主机做二个网站超级seo外链
  • 做那种事免费网站网站诊断工具
  • 网站服务器好2021最火营销方案
  • 网站域名服务器查询优化网站的目的
  • 网站建设和网站推广可以同一家做吗小红书软文推广
  • 德州网站制作公司上海优化seo公司
  • 园林景观设计公司计划书网站关键字排名优化
  • 什么语言建手机网站百家号关键词排名优化
  • html源码网站下载之家sem竞价是什么
  • 做公众号封面网站宁波好的seo外包公司
  • 百度站长工具seo查询谷歌paypal下载
  • 懒人做图网站短视频拍摄剪辑培训班
  • 顺营销官方网站全国疫情最新
  • 网络做网站如何盈利网站内部链接优化方法
  • 做动态网站 语音表达搜索关键词排名优化技术
  • 怎么套模板 网站24小时人工在线客服
  • 瓷器网站源码公司网页制作教程
  • 动态广告怎么做出来的搜索优化指的是什么
  • 网站建设网上学域名流量查询工具
  • html5高端网站建设湖南网站排名
  • 使用oss图片做网站磁力狗在线搜索
  • 如何在百度上做公司网站想要网站导航推广
  • 网络建设与网站建设提升seo排名的方法
  • 巨鹿建设银行网站首页网站seo诊断工具
  • 做齐鲁油官方网站百度关键词优化软件如何
  • 自己如何建设校园网站键词优化排名
  • asp.net.做简单的网站整站排名优化品牌
  • 学仿网站seo搜索工具栏
  • 建设公司网站多少钱产品网络营销方案