spring 3.2.4

为了给每一个controller配置一个拦截器链

  1. import com.google.common.collect.Lists;
  2. import org.aopalliance.intercept.MethodInterceptor;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.aop.TargetSource;
  6. import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
  7. import org.springframework.beans.BeansException;
  8. import org.springframework.beans.factory.BeanFactory;
  9. import org.springframework.beans.factory.InitializingBean;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.context.ApplicationContextAware;
  12. import org.springframework.core.annotation.AnnotationUtils;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16.  
  17. import java.util.List;
  18.  
  19. @Component
  20. public class AutoProxyCreator extends AbstractAutoProxyCreator implements InitializingBean {
  21.  
  22. /**
  23. * 序列化版本号
  24. */
  25. private static final long serialVersionUID = 1L;
  26. /**
  27. * 持有Spring应用上下文
  28. */
  29. private volatile ApplicationContext context = null;
  30. /**
  31. * 日志打印
  32. */
  33. private static Logger logger = LoggerFactory.getLogger(AutoProxyCreator.class);
  34.  
  35. /**
  36. * 拦截器链
  37. */
  38. private List<String> intercaptorChainList = null;
  39.  
  40. /**
  41. * 实现自动代理
  42. */
  43. @Override
  44. protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource targetSource) {
  45. // 针对Controller和RequestMapping进行拦截
  46. if (AnnotationUtils.findAnnotation(beanClass, Controller.class) != null || AnnotationUtils.findAnnotation(beanClass, RequestMapping.class) != null) {
  47. if (intercaptorChainList == null) {
  48. throw new RuntimeException("初始化拦截器链失败,异常中断程序!");
  49. }
  50. List<MethodInterceptor> methodInterceptorList = Lists.newArrayList();
  51. for(String intercaptor : intercaptorChainList){
  52. if(AnnotationUtils.findAnnotation(beanClass, ExceptedAuthValidate.class) != null ){
  53. continue;
  54. }
  55. logger.info("开始为bean:\"{}\"配置拦截器链:\"{}\"", beanClass, intercaptor);
  56. BeanFactory beanFactory = getBeanFactory();
  57. MethodInterceptor methodInterceptor = (MethodInterceptor)beanFactory.getBean(intercaptor);
  58. methodInterceptorList.add(methodInterceptor);
  59. }
  60. return methodInterceptorList.toArray();
  61. }
  62. return DO_NOT_PROXY;
  63. }
  64.  
  65. /**
  66. * 初始化拦截器链
  67. */
  68. @Override
  69. public void afterPropertiesSet() throws Exception {
  70. intercaptorChainList = Lists.newArrayList();
  71. intercaptorChainList.add("userLoginInterceptor");
  72. intercaptorChainList.add("paramValidateInterceptor");
  73. }
  74.  
  75. }

然后跑起来发现所有的controller都访问不了了

AbstractHandlerMethodMapping里打断点可以看到

被代理了之后,getType出来是代理类,代理类没有注解
 被认为不是一个handler

但是普通的AOP却没有问题,还需要了解AOP和这种代理有什么区别

记录一次因代理Controller产生的404问题的更多相关文章

  1. springboot搭建环境访问Controller层返回404

    如果启动成功,但是却访问不了你自己写的controller,报404错误,那么原因就是您写的controller没有被spring 容器扫描到 解决方案: spring boot 默认扫描您的类是 在 ...

  2. 记录下使用iis7代理node.js写的网站程序

    昨天晚上一个学弟的紧急求救,说了自己接的单子做了一个网站,使用了自己熟悉的技术——node.js+mongdb,但当看到部署环境惊呆了,是 windows+sqlserver.这些都不是关键,关键是服 ...

  3. idea创建springmvc项目创部署成功,但访问controller层报错404

    这个问题网上有很多解决问题,检查配置文件是否正确?controller注解是否扫描?项目启动是否成功等等. 访问报错404,而且后台也没错误,归根结底还是访问路径错了. 1.如图,idea配置tomc ...

  4. 记录一次MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。

    在部署到IIS7时,MVC3报了一个找不到资源的错误,文件肯定是有的,而且页面是肯定报错的,也就说内部运行错误了,而MVC把错误没有抛出来而已: 所以对症下药,发觉我的项目里面用了rexs进行多语言, ...

  5. springmvc 请求无法到达controller,出现404

    今天在配置SpringMVC时,访问项目一直出现404,无法访问. 报错: The origin server did not find a current representation for th ...

  6. 记录一次 Nginx 配置 proxy_pass 后 返回404问题

    一. Nginx 配置 proxy_pass 后 返回404问题 故障解决和定位 1.1. 问题 在一次生产涉及多次转发的配置中, 需求是下面的图: 在配置好了 proxy_pass 之后,请求 ww ...

  7. <mvc:default-servlet-handler/>导致controller失效,报404错误

    最近在做ssm框架整合的一个小项目时,页面跳转一直有404错误,也没有报错提示.然后一步一步去找,终于发现是<mvc:default-servlet-handler/>的原因.如下图所示, ...

  8. spring请求到达controller但响应404

    问题是这样的,前台发送请求的后台,后台的方法正常执行,将数据放在response.getWrite里,但在前台并没有展示数据.用浏览器的开发者工具看下请求,发现响应404. 最后网上查了查,sprin ...

  9. Java动态代理全面分析

    代理模式 解说:给某一个对象提供一个代理,并由代理对象控制对原对象的引用: 代理模式需要以下几个角色: 1  主题:规定代理类和真实对象共同对外暴露的接口: 2  代理类:专门代理真实对象的类: 3 ...

随机推荐

  1. sort和uniq去重操作【转】

    去除重复行 sort file |uniq   查找非重复行 sort file |uniq -u   查找重复行 sort file |uniq -d   统计 sort file | uniq - ...

  2. webpack打包生成多个vendor的配置方法

    用webpack打包项目的时候,一般喜欢把一些公用的库打包的vendor.js里面,比如像react,react-router,redux等. 随着引入的库越来越多,vendor文件也变得越来越大,于 ...

  3. python3+selenium入门11-窗口切换

    在打开新的浏览器窗口时,如果要定位新窗口的元素,需要先切换到这个新打开的窗口中,才能定位到该窗口下的元素. current_window_handle:获取当前句柄.可以把句柄理解成窗口的身份证 wi ...

  4. Css样式压缩、美化、净化工具 源代码

    主要功能如下: /* 美化:格式化代码,使之容易阅读 */ /* 净化:将代码单行化,并去除注释 */ /* 压缩:将代码最小化,加快加载速度 */ /* 以下是演示代码 */ /*reset beg ...

  5. 安装snap及snap常安装软件

    文章链接:https://blog.csdn.net/laomd/article/details/80710451 一.snap简介 什么是snap,snap是一种全新的软件包管理方式,它类似一个容器 ...

  6. nikto for windows(web扫描工具) 使用教程

    本文出处: 欧普软件 ----------------------------------------------------------------------------------------- ...

  7. Confluence 6 管理站点模板

    模板是一个预先定义的页面,这个预先定义的页面可以在创建新页面的时候预先载入.模板可以由用户创建也可以通过蓝图提供.请查看 Page Templates 和 Blueprints 页面中的内容. 管理员 ...

  8. ios消息机制

    ios消息机制介绍 ios 调用每一个方法的时候其实是走的ios的消息机制 举例介绍一下 创建一个Pserson类 有一个eat 对象方法 那么下面的代码可以用消息机制实现  导入消息头文件    # ...

  9. 电子书转换为PDF格式

    目录 一.mobi 转换 pdf 步骤 二.查看转换后的结果目录 三.将PDF还原文件名且移出至新目录 背景:当我们从网上下载一些电子小说或书籍的时候,一般文件的格式可能是.epub..mobi等.这 ...

  10. django----图书管理

    待完成 from django.db import models # Create your models here. class Book(models.Model): nid = models.A ...