码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet容器监听器

时间:2018-03-01 14:51:09      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:程序   gets   初始化   color   pos   toc   init   ica   ota   

package com.biniu;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @Configuration, @EnableAutoConfiguration, @ComponentScan == @SpringBootApplication
 */
@SpringBootApplication
@EnableScheduling
@ServletComponentScan
public class BaseWebApplication {
    /**
     * 程序主方法
     * @param args
     */
    public static void main(String[] args) {
        // 调用Springboot的run方法,启动Spring,自动配置servlet容器
        SpringApplication.run(BaseWebApplication.class, args);
    }
}
package com.biniu.Listener;

import com.biniu.properties.CommonProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Servlet容器监听器
 * Created by lay on 01/03/2018.
 */
@WebListener
public class ServletCtxListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("servlet context 初始化开始");

        ServletContext servletContext = servletContextEvent.getServletContext();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        CommonProperties commonProperties = context.getBean(CommonProperties.class);
        // 设置图片域名地址
        String imageUrl = commonProperties.getImageUrl();
        servletContext.setAttribute("imageUrl", imageUrl);

        System.out.println("servlet context 初始化完成");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("servlet context 销毁");
    }
}

 

Servlet容器监听器

标签:程序   gets   初始化   color   pos   toc   init   ica   ota   

原文地址:https://www.cnblogs.com/lay2017/p/8487927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!