Spring整合SpringMVC

Spring整合SpringMVC

整合spring的配置文件什么时候加载?

在web容器启动的时候,对servletContext的生命周期创建监听器,该监听器监听servletContext个生命周期。


servletListener几种实现方式

直接实现原生的 ServletContextListener

package com.haiyang.springmybatis;

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

@WebListener
public class ServletListener implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent sce) {

        //这里进行初始化数据
        System.out.println("这里进行了监听器的初始化对象");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

实现spring框架的Applicationlistener

package com.haiyang.springmybatis;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class SpringServletListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        System.out.println("applicationEvent = " + applicationEvent.getTimestamp());
        System.out.println(applicationEvent.getClass().getName());
    }
}

以上两种都可以实现对servletContext监听,但是他们是有加载顺序的。原生的servletContextListener要优先于ApplicationContext。

springboot可以实现在各个阶段的事件监听

package com.haiyang.springmybatis;

import com.alibaba.fastjson.JSON;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;

import java.util.Map;


public class ApplicationListenerImpl implements SpringApplicationRunListener {
    private final SpringApplication application;
    private final String[] args;

    public ApplicationListenerImpl(SpringApplication application, String[] args) {
        this.application = application;
        this.args = args;
    }

    @Override
    public void starting(ConfigurableBootstrapContext bootstrapContext) {
        System.out.println("SpringApplicationRunListener starting .....启动中");
    }

    @Override
    public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
        System.out.println("环境建立好时候");
        Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
        System.out.println("JSON.toJSONString(systemEnvironment) = " + JSON.toJSONString(systemEnvironment));
        Map<String, Object> systemProperties = environment.getSystemProperties();
        System.out.println("JSON.toJSONString(systemProperties) = " + JSON.toJSONString(systemProperties));
        MutablePropertySources propertySources = environment.getPropertySources();
        System.out.println("propertySources.toString() = " + propertySources.toString());
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("上下文建立好的时候");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("上下文载入配置时候");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("started...");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {

        System.out.println("running");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {

        System.out.println("failed");
    }


}

除了以上操作外,还需要在resources/META-INF/spring.factories声明applicationRunListener,如下:

org.springframework.boot.SpringApplicationRunListener=\
com.haiyang.springmybatis.ApplicationListenerImpl
posted @   Eular  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示