zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
package kernel.web;
 
import java.util.Enumeration;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
 
/**
 * @author JORGE
 * @description WEB应用监听器
 */
public class SpringWebContextListener extends ContextLoader implements ServletContextListener {
    /**
     * SLF4J日志工具
     */
    private static final Logger logger=LoggerFactory.getLogger(SpringWebContextListener.class);
    
    public SpringWebContextListener(WebApplicationContext context) {
        super(context);
    }
 
    public SpringWebContextListener() {
        super.setContextInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {
            public void initialize(ConfigurableApplicationContext applicationContext) {
                if(applicationContext instanceof WebApplicationContext) {
                    ApplicationUtil.setApplicationContext((WebApplicationContext)applicationContext);
                }
            }
        });
    }
    
    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext servletContext=event.getServletContext();
        if (null==servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)) {
            super.initWebApplicationContext(servletContext);
        }
        ApplicationUtil.setServletContext(servletContext);
    }
 
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        ServletContext servletContext=event.getServletContext();
        closeWebApplicationContext(servletContext);
        
        Enumeration<String> attrNames = servletContext.getAttributeNames();
        while (attrNames.hasMoreElements()) {
            String attrName = attrNames.nextElement();
            if (!attrName.startsWith("org.springframework.")) continue;
            Object attrValue = servletContext.getAttribute(attrName);
            if (!(attrValue instanceof DisposableBean)) continue;
            try {
                ((DisposableBean) attrValue).destroy();
            }catch (Throwable e) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Invocation of destroy method failed on ServletContext "+"attribute with name '" + attrName + "'", e);
                }
            }
        }
    }
    
}