<listener>
<listener-class>com.java.listener.web.AppServletContextListenerlistener-class>
listener>
package com.java.listener.web;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class AppServletContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent event) {
// TODO serletContext销毁
}
@Override
public void contextInitialized(ServletContextEvent event) {
// TODO serletContext创建
}
}
<listener>
<listener-class>com.java.listener.web.AppServletRequestListenerlistener-class>
listener>
package com.java.listener.web;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class AppServletRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent request) {
// TODO request销毁事件
}
@Override
public void requestInitialized(ServletRequestEvent request) {
// TODO request初始化事件
}
}
<listener>
<listener-class>com.java.listener.web.AppHttpSessionListenerlistener-class>
listener>
package com.java.listener.web;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class AppHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent session) {
// TODO session创建
}
@Override
public void sessionDestroyed(HttpSessionEvent session) {
// TODO session销毁
}
}
当我们创建类并实现这些接口,就会调用这些接口的方法,经过测试这些接口bean的调用顺序并没有绝对关系(有部分有关系),谁配在前面谁先加载并执行实现方法
public class SpringApplicationContextAware implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO ApplicationContextAware接口方法执行 setApplicationContext
//通常这个方法用来获取 applicationContext 类以便后续能通过 applicationContext 获取bean对象 等操作
}
}
public class AppBeanFactoryAware implements BeanFactoryAware {
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
//可以获取 beanFactory 对象 进行操作
}
}
public class AppBeanNameAware implements BeanNameAware{
@Override
public void setBeanName(String name) {
// TODO Auto-generated method stub
}
}
public class AppInitializingBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
}
}
public class AppDiposableBean implements DisposableBean {
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
}
}
public class AppBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
//同样可以获取beanFactory
}
}
public class AppBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO 用于bean被初始化前
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO 用于bean被初始化后
return bean;
}
}
public class AppInstantiationAwareBeanPostProcessorAdapter extends InstantiationAwareBeanPostProcessorAdapter {
@Override
public Class> predictBeanType(Class> beanClass, String beanName) {
// TODO Auto-generated method stub
return super.predictBeanType(beanClass, beanName);
}
@Override
public Constructor>[] determineCandidateConstructors(Class> beanClass, String beanName) throws BeansException {
// TODO Auto-generated method stub
return super.determineCandidateConstructors(beanClass, beanName);
}
@Override
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
return super.getEarlyBeanReference(bean, beanName);
}
@Override
public Object postProcessBeforeInstantiation(Class> beanClass, String beanName) throws BeansException {
// TODO bean对象实例化前调用
return super.postProcessBeforeInstantiation(beanClass, beanName);
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
// TODO bean对象实例化后调用
return super.postProcessAfterInstantiation(bean, beanName);
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
String beanName) throws BeansException {
// TODO 设置bean的属性时调用
return super.postProcessPropertyValues(pvs, pds, bean, beanName);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO bean对象初始化时调用
return super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO bean对象初始化后调用
return super.postProcessAfterInitialization(bean, beanName);
}
}
public class AppContextLoaderListener extends ContextLoaderListener {
@Override
public void closeWebApplicationContext(ServletContext servletContext) {
// TODO Auto-generated method stub
super.closeWebApplicationContext(servletContext);
}
@Override
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
// TODO 初始化 ServletContext 上下文 这个方法在contextInitialized 方法之前调用
return super.initWebApplicationContext(servletContext);
}
@Override
public void contextInitialized(ServletContextEvent event) {
// TODO 上下文初始化成功
super.contextInitialized(event);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// TODO Auto-generated method stub
super.contextDestroyed(event);
}
}
<web-app>
<display-name>Archetype Created Web Applicationdisplay-name>
<listener>
<listener-class>com.java.listener.web.AppServletContextListenerlistener-class>
listener>
<listener>
<listener-class>com.java.listener.web.AppServletRequestListenerlistener-class>
listener>
<listener>
<listener-class>com.java.listener.web.AppHttpSessionListenerlistener-class>
listener>
<listener>
<listener-class>com.java.listener.spring.AppContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/applicationContext.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<servlet>
<servlet-name>InitServletservlet-name>
<display-name>InitServletdisplay-name>
<description>description>
<servlet-class>com.java.servlet.InitServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="springApplicationContextAware" class="com.java.listener.spring.SpringApplicationContextAware">bean>
<bean id="appBeanFactoryAware" class="com.java.listener.spring.AppBeanFactoryAware">bean>
<bean id="appBeanNameAware" class="com.java.listener.spring.AppBeanNameAware">bean>
<bean id="appInitializingBean" class="com.java.listener.spring.AppInitializingBean">bean>
<bean id="appDiposableBean" class="com.java.listener.spring.AppDiposableBean">bean>
<bean id="appBeanFactoryPostProcessor" class="com.java.listener.spring.AppBeanFactoryPostProcessor">bean>
<bean id="appInstantiationAwareBeanPostProcessorAdapter" class="com.java.listener.spring.AppInstantiationAwareBeanPostProcessorAdapter">bean>
<bean id="appBeanPostProcessor" class="com.java.listener.spring.AppBeanPostProcessor">bean>
beans>