zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
    人生没有彩排,每天都是现场直播!
  • 相关阅读:
    html的输出&,空格,大小于号
    html如何修改hr水平直线的粗细
    LODOP指定window默认打印机和临时默认打印机
    微软面试题: 找出二叉树上任意两个结点的最近共同父结点。
    说说自己对hibernate一级、二级、查询、缓存的理解。
    MySql中添加用户,新建数据库,用户授权,删除用户,修改密码
    修改MySQL的默认密码的四种小方法
    java中Scanner的nextLine()和next()的区别
    JAVA中String字符串比较equals()和equalsIgnoreCase()的区别
    HashMap与HashTable的区别
  • 原文地址:https://www.cnblogs.com/northern-light/p/10498551.html
Copyright © 2011-2022 走看看