• springboot添加拦截器 监听等设置


    1.添加拦截器

    package com.jy.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import yaycrawler.common.interceptor.SignatureSecurityInterceptor;
    
    
    @Configuration
    public class WebAppConfig extends WebMvcConfigurerAdapter {
        
        /**
            添加拦截器
      
    WebMvcConfigurerAdapter 显示过期,则使用继承至org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
    */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MySignatureSecurityInterceptor()).addPathPatterns("/admin/**","/manager/**"); } }

    2、创建监听器

    package com.jy.listener;
    
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.web.context.WebApplicationContext;
    
    /**
     * 
     * 启动worker监听器 
     */
    public class MyRegisterListener implements ApplicationListener<ContextRefreshedEvent> {
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            WebApplicationContext context = (WebApplicationContext) contextRefreshedEvent.getApplicationContext();
            WorkerContext.webApplicationContext = context;
            Userservice userservice = context.getBean(Userservice.class);
            //做其他操作
           
        }
    }

    3、添加监听器

    package com.jy;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.context.web.SpringBootServletInitializer;
    import yaycrawler.worker.listener.WorkerRegisterListener;
    
    /**
     * 启动时,向master注册 
     */
    public class ServletInitializer extends SpringBootServletInitializer {
       
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //如上第二部创建的监听器 application.listeners(
    new MyRegisterListener()); return application.sources(Application.class); } }

    4、项目启动时初始化操作(例如初始化项目根路径)

    package com.jy.context;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    import org.springframework.util.ResourceUtils;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    
    @Component
    public class MyContextAware implements ApplicationContextAware {
    
        private Logger logger = LoggerFactory.getLogger(MyContextAware.class);
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            
            //获取跟目录
            File path = null;
            try {
                path = new File(ResourceUtils.getURL("classpath:").getPath());
                if(!path.exists()) {
                    path = new File("");
                }
                //自行定义的类Global,ROOT为static 变量;项目启动时初始化项目路径 
                Global.ROOT = path.getAbsolutePath()+File.separator;
    
                /*
                    在开发测试模式时,得到的地址为:{项目跟目录}/target/classes/
                    在打包成jar正式发布时,得到的地址为:{发布jar包目录}/
                */
    
            } catch (FileNotFoundException e) {
                logger.error("FileNotFoundException",e);
            }
            logger.info("Global.ROOT {}",Global.ROOT);
        }
    
    }
    人生没有彩排,每天都是现场直播!
  • 相关阅读:
    jquery 插件 lettering.js
    css中一些生僻却好用的属性
    mui-5+api(plus)加载顺序
    网易云音乐mp3外链、真实地址下载方法
    移动端a标签的妙用(拨号、短信、邮件等)
    Android上架mui-app
    ios-app证书配置、打包提交
    前端兼容性问题
    米拓CMS学习总结
    1) 链表顺序存储---之二代码
  • 原文地址:https://www.cnblogs.com/northern-light/p/10498551.html
Copyright © 2020-2023  润新知