方法1
以下方法适用于容器启动时读取一些自己定义的配置文件内容
自己写一个listener(如 TestContextLoaderListener) 继承 ContextLoaderListener
web.xml里的listener配置由
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
改成
<listener>
<listener-class>com.zw.TestContextLoaderListener</listener-class>
</listener>
一下是ContextLoaderListener源码
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.context;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}.
* Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}.
*
* <p>This listener should be registered after
* {@link org.springframework.web.util.Log4jConfigListener}
* in <code>web.xml</code>, if the latter is used.
*
* @author Juergen Hoeller
* @since 17.02.2003
* @see org.springframework.web.util.Log4jConfigListener
*/
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
* @deprecated in favor of simply subclassing ContextLoaderListener itself
* (which extends ContextLoader, as of Spring 3.0)
*/
@Deprecated
protected ContextLoader createContextLoader() {
return null;
}
/**
* Return the ContextLoader used by this listener.
* @return the current ContextLoader
* @deprecated in favor of simply subclassing ContextLoaderListener itself
* (which extends ContextLoader, as of Spring 3.0)
*/
@Deprecated
public ContextLoader getContextLoader() {
return this.contextLoader;
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}
我们要重写 contextInitialized这个方法
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
//加入自己的操作
}
方法2
让你的service实现ApplicationListener接口
在onApplicationEvent(E event)方法中 写你自己的操作
ContextRefreshedEvent 这个时间是bean初始化后调用的
onApplicationEvent(ContextRefreshedEvent event){
//加入自己的操作
}
也可以这么写
onApplicationEvent(ApplicationEvent event){
if(event instanceof ContextRefreshedEvent){
//加入自己的操作
}
}
event事件介绍
ContextStoppedEvent context停止事件
ContextRefreshedEvent context bean加载完事件
ContextStartedEvent context开始事件
ContextClosedEvent context关闭事件