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

在spring的web项目中,我们可以介入Spring的启动过程。

我们希望在Spring容器将所有的Bean都初始化完成之后,做一些操作,这个时候我们就可以实现一个接口:

方法一、

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

  



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

1
2
class = "com.leadsoft.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) {
    //root application context 没有parent,他就是老大.
    if (event.getApplicationContext().getParent() ==  null ){
        //需要执行的逻辑代码,当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
  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 {
    }


    你可能感兴趣的:(Java)