spring 容器初始化執行某些方法
来源:互联网 发布:mac 战网 改台服 编辑:程序博客网 时间:2024/06/05 07:39
https://www.cnblogs.com/rollenholt/p/3612440.html
方式1
项目用到了ApplicationContextAware,通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。
我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。
使用方法如下:
1.实现ApplicationContextAware接口:
2.在Spring的配置文件中配置这个类,Spring容器会在加载完Spring容器后把上下文对象调用这个对象中的setApplicationContext方法:
3.在web项目中的web.xml中配置加载Spring容器的Listener:
4.在项目中即可通过这个SpringContextHelper调用getBean()方法得到Spring容器中的对象了。
方式2
在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查。
比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出哪个文件的xml文件使用了这个函数。
而在Spring的web项目中,我们可以介入Spring的启动过程。我们希望在Spring容器将所有的Bean都初始化完成之后,做一些操作,这个时候我们就可以实现一个接口:
package
com.yk.test.executor.processor
public
class
InstantiationTracingBeanPostProcessor
implements
ApplicationListener<ContextRefreshedEvent> {
@Override
public
void
onApplicationEvent(ContextRefreshedEvent event) {
//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。
}
}
同时在Spring的配置文件中,添加注入:
<!-- 当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初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码
如下:
@Override
public
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:
* <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 {
}
- spring 容器初始化執行某些方法
- spring容器的三种初始化方法
- Spring容器初始化后执行的方法
- 初始化spring容器的几种方法
- spring初始化容器
- Spring初始化容器
- Spring初始化容器
- Spring获得初始化容器
- Spring容器初始化
- spring容器重复初始化
- Spring IoC容器初始化
- Spring Ioc容器初始化
- spring初始化容器
- Spring容器初始化
- Spring容器初始化过程
- Spring-IoC容器初始化
- spring容器的初始化
- Spring容器初始化过程
- setprecision、fixed、showpoint的用法总结 (经典!!超经典!!)
- HBase RPC理解之三
- 程序编程的本质
- 修改ubuntu默认键盘布局
- python 操作 mysql
- spring 容器初始化執行某些方法
- httpclient4.5 的一些细节
- Unity3D研究院之使用 C#合成解析XML与JSON
- Dubbo之 The prefix "context" for element "context:component-scan" is not bound
- codeforces D. Levko and Array(二分加dp) 挺好的一个题
- Apache OpenOffice
- pytorch对可变序列的处理
- 万树IT:零基础也能学习安卓开发
- Django技术详解(五)开发Models