详解SpringBoot开发使用@ImportResource注解影响 *

作者:袁老板 时间:2021-08-02 13:49:53 

问题描述

今天在给SpringBoot项目配置 * 的时候发现怎么都进不到 * 的方法里面,在搜索引擎上看了无数篇关于配置 * 的文章都没有找到解决方案。

就在我准备放弃的时候,在 CSDN 上发现了一篇文章,说的是SpringBoot 用了@ImportResource 配置的 * 就不起作用了。于是我就赶紧到Application启动类看了一眼,果然项目中使用了@ImportResource 注解用于配置系统的参数。

代码如下:

启动类配置


package com.xx.xxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@EnableDiscoveryClient
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,
   ThymeleafAutoConfiguration.class})
@SpringBootApplication
// 注意这里 !!!!
@ImportResource(locations={"classpath:config/application-*.xml"})
@EnableHystrix
public class Application extends SpringBootServletInitializer {

public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 }
}

* 配置


package com.xx.xxx.config;

import com.example.springbootdemo.Interceptor.LoginInterceptor;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

/**
  * * (用户登录验证)
  * @param registry
  */
 @Override
 public void addInterceptors(InterceptorRegistry registry) {
   // addPathPatterns 用于添加拦截规则
   // excludePathPatterns 用户排除拦截
   registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user","/login");
   super.addInterceptors(registry);
 }
}

* 实现


package com.xx.xxx.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

private final static Logger LOGGER = LoggerFactory.getLogger(LoginInterceptor.class);

@Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   LOGGER.info("******进来了******");
   return true;
 }

@Override
 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
 }

@Override
 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
 }
}

具体为什么使用@ImportResource注解会影响 * 的配置,如果有机会研究一下源码或许能够找到答案。

PS : SpringBoot 版本 1.5.2

来源:https://www.cnblogs.com/yuansc/p/9077509.html

标签:Spring,Boot,@ImportResource
0
投稿

猜你喜欢

  • Java 控制线程的方法

    2022-10-14 15:20:22
  • Java中对象的比较操作实例分析

    2023-03-05 03:58:09
  • Java设计模式之监听器模式实例详解

    2022-10-04 02:51:44
  • Redis使用RedisTemplate模板类的常用操作方式

    2023-01-30 08:33:40
  • Java中类的加载顺序执行结果

    2022-03-12 18:12:35
  • C#中List集合使用Max()方法查找到最大值的实例

    2023-12-01 08:03:32
  • java private关键字用法实例

    2022-01-16 10:08:06
  • Java一个简单的红包生成算法

    2023-12-12 10:56:50
  • Java中批处理框架spring batch详细介绍

    2023-10-19 16:54:21
  • 一个JAVA小项目--Web应用自动生成Word

    2022-04-30 07:19:56
  • Java实现显示指定类型的文件

    2021-10-26 11:30:37
  • Java编程实现向文本文件中读取数据之Scanner用法示例

    2022-08-01 03:38:57
  • java 对象参数去空格方式代码实例

    2023-11-27 09:49:34
  • Java读取json数据并存入数据库的操作代码

    2023-09-23 06:00:57
  • JVM类加载机制原理及用法解析

    2021-08-17 00:22:29
  • Java如何使用httpclient检测url状态及链接是否能打开

    2022-07-03 21:23:25
  • 一文带你了解C#中抽象方法与虚方法的区别

    2023-07-23 00:14:32
  • java基础的详细了解第五天

    2023-06-02 16:54:04
  • Java方法的返回值及注意事项小结

    2022-10-02 16:55:28
  • SpringIOC容器Bean的作用域及生命周期实例

    2023-10-01 11:21:34
  • asp之家 软件编程 m.aspxhome.com