spring 3.2.4

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

import com.google.common.collect.Lists;
import org.aopalliance.intercept.MethodInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Component
public class AutoProxyCreator extends AbstractAutoProxyCreator implements InitializingBean { /**
* 序列化版本号
*/
private static final long serialVersionUID = 1L;
/**
* 持有Spring应用上下文
*/
private volatile ApplicationContext context = null;
/**
* 日志打印
*/
private static Logger logger = LoggerFactory.getLogger(AutoProxyCreator.class); /**
* 拦截器链
*/
private List<String> intercaptorChainList = null; /**
* 实现自动代理
*/
@Override
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource targetSource) {
// 针对Controller和RequestMapping进行拦截
if (AnnotationUtils.findAnnotation(beanClass, Controller.class) != null || AnnotationUtils.findAnnotation(beanClass, RequestMapping.class) != null) {
if (intercaptorChainList == null) {
throw new RuntimeException("初始化拦截器链失败,异常中断程序!");
}
List<MethodInterceptor> methodInterceptorList = Lists.newArrayList();
for(String intercaptor : intercaptorChainList){
if(AnnotationUtils.findAnnotation(beanClass, ExceptedAuthValidate.class) != null ){
continue;
}
logger.info("开始为bean:\"{}\"配置拦截器链:\"{}\"", beanClass, intercaptor);
BeanFactory beanFactory = getBeanFactory();
MethodInterceptor methodInterceptor = (MethodInterceptor)beanFactory.getBean(intercaptor);
methodInterceptorList.add(methodInterceptor);
}
return methodInterceptorList.toArray();
}
return DO_NOT_PROXY;
} /**
* 初始化拦截器链
*/
@Override
public void afterPropertiesSet() throws Exception {
intercaptorChainList = Lists.newArrayList();
intercaptorChainList.add("userLoginInterceptor");
intercaptorChainList.add("paramValidateInterceptor");
} }

然后跑起来发现所有的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. hdfs命令get或者put提示找不到目录或文件

    今天用hdfs命令出现个诡异情况: hadoop fs -put a.txt /user/root/ put: `a.txt': No such file or directory 用get命令存在相 ...

  2. Go语言中的map

    map是一个集合,可以使用类似处理数组和切片的方式迭代map中的元素.但map是无序的集合.无序的原因是map的实现使用了散列表. map的创建并初始化主要是两种方式: 1.内置的make函数 2.使 ...

  3. DevExpress VCL 的 cxDBTreeList 的使用方法

    DevExpress VCL 的 cxDBTreeList 的使用方法:(假设控件名为: WBSTree) 1.控件WBSTree 通过绑定  DataSet 获取数据记录(Nodes),通过 Col ...

  4. python中input和raw_input函数

    python input() 相等于 eval(raw_input(prompt)) ,用来获取控制台的输入. raw_input() 将所有输入作为字符串看待,返回字符串类型.而 input() 在 ...

  5. 题解-PKUWC2018 Minimax

    Problem loj2537 Solution pkuwc2018最水的一题,要死要活调了一个多小时(1h59min) 我写这题不是因为它有多好,而是为了保持pkuwc2018的队形,与这题类似的有 ...

  6. 【转】C++标准转换运算符reinterpret_cast

    reinterpret_cast<new_type> (expression) reinterpret_cast运算符是用来处理无关类型之间的转换:它会产生一个新的值,这个值会有与原始参数 ...

  7. web-font 个人学习小总结

    个人觉得 web-font  分为两种: 第一种就是真正文本字体,客户端没有安装的字体通过远程加载字体来实现特殊字体提高用户体验: icodon.com : http://icodon.com/goo ...

  8. LabVIEW与Arduino的连接

    labVIEW和Arduino的连接有两种方法: 第一种方法是用Arduino的IDE去打开位于labVIEW文件目录下面的(C:\Program Files (x86)\National Instr ...

  9. Vue -cli 入门 --项目搭建(一)

    一. 安装node.js环境. 在node.js官网下载稳定版本(https://nodejs.org/en/) 下载完成后点击安装,安装过程很简单,一直next即可,安装完成会自动添加node及np ...

  10. appium+java报错之nodejs报错

    $ gulp(node:784) fs: re-evaluating native module sources is not supported. If you areusing the grace ...