当spring 容器初始化完成后执行某个方法

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

当spring 容器初始化完成后执行某个方法


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

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

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

1
2
3
4
5
6
7
package  com.yk.test.executor.processor
public  class  InstantiationTracingBeanPostProcessor  implements  ApplicationListener<ContextRefreshedEvent> {
@Override
public  void  onApplicationEvent(ContextRefreshedEvent event) {
//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。
}
}

  



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

1
2
<!-- 当Spring容器启动完成后执行下面的这个Bean -->
<bean  class = "com.yk.test.executor.processor.InstantiationTracingBeanPostProcessor" />

  

  

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

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

如下:

 

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

  

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

注解描述如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
  * 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:
  * <p>
  * <ul>
  * <li>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.</li>
  * <li>The method defined on an interceptor class MUST HAVE one of the
  * following signatures:
  * <p>
  * void <METHOD>(InvocationContext)
  * <p>
  * Object <METHOD>(InvocationContext) throws Exception
  * <p>
  * <i>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.</i>
  * </li>
  * <li>The method defined on a non-interceptor class MUST HAVE the
  * following signature:
  * <p>
  * void <METHOD>()
  * </li>
  * <li>The method on which PostConstruct is applied MAY be public, protected,
  * package private or private.</li>
  * <li>The method MUST NOT be static except for the application client.</li>
  * <li>The method MAY be final.</li>
  * <li>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.</li></ul>
  * @since Common Annotations 1.0
  * @see javax.annotation.PreDestroy
  * @see javax.annotation.Resource
  */
@Documented
@Retention  (RUNTIME)
@Target (METHOD)
public  @interface  PostConstruct {
}

  


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/p/3612440.html,如需转载请自行联系原作者

相关文章
|
4天前
|
存储 安全 算法
Java容器及其常用方法汇总
Java Collections框架提供了丰富的接口和实现类,用于管理和操作集合数据。
Java容器及其常用方法汇总
|
3天前
|
XML Java 数据格式
Spring容器的本质
本文主要讨论Spring容器最核心的机制,用最少的代码讲清楚Spring容器的本质。
|
2月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
74 6
|
3月前
|
缓存 Java Spring
实战指南:四种调整 Spring Bean 初始化顺序的方案
本文探讨了如何调整 Spring Boot 中 Bean 的初始化顺序,以满足业务需求。文章通过四种方案进行了详细分析: 1. **方案一 (@Order)**:通过 `@Order` 注解设置 Bean 的初始化顺序,但发现 `@PostConstruct` 会影响顺序。 2. **方案二 (SmartInitializingSingleton)**:在所有单例 Bean 初始化后执行额外的初始化工作,但无法精确控制特定 Bean 的顺序。 3. **方案三 (@DependsOn)**:通过 `@DependsOn` 注解指定 Bean 之间的依赖关系,成功实现顺序控制,但耦合性较高。
140 4
实战指南:四种调整 Spring Bean 初始化顺序的方案
|
2月前
|
存储 Prometheus 监控
Docker容器内进行应用调试与故障排除的方法与技巧,包括使用日志、进入容器检查、利用监控工具及检查配置等,旨在帮助用户有效应对应用部署中的挑战,确保应用稳定运行
本文深入探讨了在Docker容器内进行应用调试与故障排除的方法与技巧,包括使用日志、进入容器检查、利用监控工具及检查配置等,旨在帮助用户有效应对应用部署中的挑战,确保应用稳定运行。
85 5
|
2月前
|
安全 Java 开发者
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
50 1
|
2月前
|
负载均衡 网络协议 算法
Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式
本文探讨了Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式,以及软件负载均衡器、云服务负载均衡、容器编排工具等实现手段,强调两者结合的重要性及面临挑战的应对措施。
126 3
|
4月前
|
Kubernetes 监控 Cloud Native
|
3月前
|
Kubernetes 容器 Perl
【赵渝强老师】K8s中Pod中的初始化容器
Kubernetes的Pod包含业务容器、基础容器、初始化容器和临时容器。初始化容器在业务容器前运行,用于执行必要的初始化任务。本文介绍了初始化容器的作用、配置方法及优势,并提供了一个示例。
|
3月前
|
前端开发 Java Docker
使用Docker容器化部署Spring Boot应用程序
使用Docker容器化部署Spring Boot应用程序

热门文章

最新文章