奇怪的是,我自己搭建的springmvc框架就只执行了一次,但是在项目中,主执行了2次。
为什么spring AOP执行2次?
1、去掉@Component就可以了。【是否去掉@Component,要看spring的其它配置,换个文件夹,没有@Component反而不会执行aop了。】
2、日志的原因,请参照:http://stackoverflow.com/questions/18731520/spring-aop-controller-executing-twice
http://stackoverflow.com/questions/11516092/spring-aop-advice-called-twice
部分代码如下:
/**
*
* @author arthur.paincupid.lee
* @since 2016.04.17
*/
@Aspect
@Component
public class SysLogWithOutAnn {
private static final Logger logger = LoggerFactory
.getLogger(SysLogWithOutAnn.class);
private static String[] types = { "java.lang.Integer", "java.lang.Double",
"java.lang.Float", "java.lang.Long", "java.lang.Short",
"java.lang.Byte", "java.lang.Boolean", "java.lang.Char",
"java.lang.String", "int", "double", "long", "short", "byte",
"boolean", "char", "float" };
@Pointcut("execution(* com.paincupid.springmvc.*.controller.*.search*(..))")
public void searchControllerCall() {
}
@AfterReturning(value = "searchControllerCall()", argNames = "rtv", returning = "rtv")
public void searchControllerCallCalls(JoinPoint joinPoint, Object rtv)
throws Throwable {
System.out.println("---------------");
String classType = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(classType);
I don't annotate aspects with @Component annoation, try to remove it, maybe because of that is being processed twice by Spring.
参考:http://stackoverflow.com/questions/7900905/spring-aop-advice-is-called-twice
http://stackoverflow.com/questions/18731520/spring-aop-controller-executing-twice
http://stackoverflow.com/questions/11516092/spring-aop-advice-called-twice
转载请注明出处:http://blog.csdn.net/paincupid/article/details/51190501