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 {
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM