博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
当spring 容器初始化完成后执行某个方法
阅读量:6150 次
发布时间:2019-06-21

本文共 3510 字,大约阅读时间需要 11 分钟。

在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查。

比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出哪个文件的xml文件使用了这个函数。

而在Spring的web项目中,我们可以介入Spring的启动过程。我们希望在Spring容器将所有的Bean都初始化完成之后,做一些操作,这个时候我们就可以实现一个接口:

package com.yk.test.executor.processorpublic class InstantiationTracingBeanPostProcessor implements ApplicationListener
{@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。}}

  

  同时在Spring的配置文件中,添加注入:

  

  

但是这个时候,会存在一个问题,在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。

这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码

如下:

 

@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {if(event.getApplicationContext().getParent() == null){//root application context 没有parent,他就是老大.//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。}}

  

其实更简单的方法是使用注解:`@PostConstruct`,只需要在需要启动的时候执行的方法上标注这个注解就搞定了。

注解描述如下:

 

/* * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */package javax.annotation;import java.lang.annotation.*;import static java.lang.annotation.ElementType.*;import static java.lang.annotation.RetentionPolicy.*;/** * The PostConstruct annotation is used on a method that needs to be executed * after dependency injection is done to perform any initialization. This * method MUST be invoked before the class is put into service. This * annotation MUST be supported on all classes that support dependency * injection. The method annotated with PostConstruct MUST be invoked even * if the class does not request any resources to be injected. Only one * method can be annotated with this annotation. The method on which the * PostConstruct annotation is applied MUST fulfill all of the following * criteria: * 

*

    *
  • The method MUST NOT have any parameters except in the case of * interceptors in which case it takes an InvocationContext object as * defined by the Interceptors specification.
  • *
  • The method defined on an interceptor class MUST HAVE one of the * following signatures: *

    * void

    (InvocationContext) *

    * Object

    (InvocationContext) throws Exception *

    * Note: A PostConstruct interceptor method must not throw application * exceptions, but it may be declared to throw checked exceptions including * the java.lang.Exception if the same interceptor method interposes on * business or timeout methods in addition to lifecycle events. If a * PostConstruct interceptor method returns a value, it is ignored by * the container. *

  • *
  • The method defined on a non-interceptor class MUST HAVE the * following signature: *

    * void

    () *

  • *
  • The method on which PostConstruct is applied MAY be public, protected, * package private or private.
  • *
  • The method MUST NOT be static except for the application client.
  • *
  • The method MAY be final.
  • *
  • If the method throws an unchecked exception the class MUST NOT be put into * service except in the case of EJBs where the EJB can handle exceptions and * even recover from them.
* @since Common Annotations 1.0 * @see javax.annotation.PreDestroy * @see javax.annotation.Resource */@Documented@Retention (RUNTIME)@Target(METHOD)public @interface PostConstruct {}

  

转载地址:http://nogya.baihongyu.com/

你可能感兴趣的文章
JDBC规范(转)
查看>>
centos7下安装nginx
查看>>
zabbix 监控docker
查看>>
传播行为
查看>>
CCF NOI1140 高精度乘法
查看>>
如何制定绩效计划
查看>>
安装Microsoft Dynamics CRM 2011时出现“Microsoft.Crm.Setup.Common.Analyzer+CollectAction 操作失败”的解决办法...
查看>>
js异步编程终级解决方案 async/await
查看>>
Android Studio 更新
查看>>
MP4命令行处理
查看>>
Solaris下开发64位程序的注意事项
查看>>
Android Fragment 你应该知道的一切
查看>>
【Visual C++】游戏开发笔记十六 讲解一个完整的回合制游戏demo
查看>>
Android Studio修改apk打包生成名称
查看>>
【Git】本地与GitHub同步
查看>>
SQL Server 移动master 数据库
查看>>
推荐一款实用的编辑器
查看>>
windows 批处理文件中引用日期
查看>>
sharepoint权限集中管理工具
查看>>
Google DevTools Explanation
查看>>