使用@Before的AOP spring获取java.lang.StackOverflowError

时间:2016-11-04 13:21:58

标签: spring spring-aop

我正在努力学习AOP弹簧。所以我已经安装了AspectJ插件并在Luna eclipse中创建了AspectJ项目,这里是Project Explore的快照:

[项目探索] [1] [1]:https://i.stack.imgur.com/el0TZ.jpg

这是我的代码:

AopMain.java

 package org.koushik.javabrains;

import org.koushik.javabrains.service.ShapeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopMain {

public static void main(String[] args) {
    ApplicationContext ctx = new
            ClassPathXmlApplicationContext("spring.xml");
    ShapeService shapeService = ctx.getBean("shapeService",ShapeService.class);
    System.out.println(shapeService.getCircle().getCircleName());
  }

}

LoggingAspect.java

 package org.koushik.javabrains.aspect;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Pointcut;

 @Aspect
 public class LoggingAspect {
    @Before( "allCircleMethod()")
    public void loggingAdvice(JoinPoint joinPoint){
    System.out.println(joinPoint.toString());
 }
//@Before("args(name)")
//public void stringArgumentMethods(String name){
//      System.out.println("name: "+name);
//}


@Pointcut("execution(* get*())")
public void allGetters(){}

@Pointcut("within(org.koushik.javabrains.model.Circle)")
public void allCircleMethod(){}
}

Circle.java

  package org.koushik.javabrains.model;

  public class Circle {

    private String circleName;

    public String getCircleName() {
    return circleName;
    }

  public void setCircleName(String circleName) {
    this.circleName = circleName;
  }
}

Triangle.java

package org.koushik.javabrains.model;

public class Triangle {
    private String triangleName;

    public String getTriangleName() {
       return triangleName;
    }

   public void setTriangleName(String triangleName) {
      this.triangleName = triangleName;
   }
}

ShapeServices.java

 package org.koushik.javabrains.service;

import org.koushik.javabrains.model.Circle;
import org.koushik.javabrains.model.Triangle;

public class ShapeService {
    private Circle circle;
    private Triangle triangle;

   public Circle getCircle() {
       return circle;
   }
   public void setCircle(Circle circle) {
     this.circle = circle;
   }
  public Triangle getTriangle() {
      return triangle;
  }
  public void setTriangle(Triangle triangle) {
     this.triangle = triangle;
  }
}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
   <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean name="triangle" class="org.koushik.javabrains.model.Triangle">
     <property name="name" value ="Triangle name"></property>
   </bean>

  <bean name="circle" class="org.koushik.javabrains.model.Circle">
      <property name="name" value ="Circle Name"></property>
  </bean>
   <bean name="shapeService"    class="org.koushik.javabrains.service.ShapeService" autowire="byName"/>
   <bean name ="loggingAspect" class ="org.koushik.javabrains.aspect.LoggingAspect"/>

 </beans>

代码正常运行,无需使用 LoggingAspect.java

@Before("args(name)")
    public void stringArgumentMethods(String name){
         System.out.println("name: "+name);
  }

但是当我添加它时,我将得到java.lang.stackOverflowError:

Exception in thread "main" java.lang.StackOverflowError
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)
    at org.koushik.javabrains.aspect.LoggingAspect.stringArgumentMethods(LoggingAspect.java)

谁能告诉我为什么会这样?怎么解决呢?

1 个答案:

答案 0 :(得分:1)

来自java docs,

StackOverFlowError - 什么:

  

因应用程序递归而发生堆栈溢出时抛出   太深了。

这意味着内存(堆栈)已满,并且没有空间可以进一步存储。

<强>为什么:

大多数情况下,这种情况是由递归/深度调用方法创建的。

您的情况@Before("args(name)") - 此行尝试使用参数&#34; name&#34;,查找所有方法发现自己会导致递归调用和stackoverflow错误。因为stringArgumentMethods(String name)也有参数name

public void stringArgumentMethods(String name){
     System.out.println("name: "+name);

}

如何解决:

重写AspectJ表达式 - @Before("args(name)")

或者

重命名参数,如stringArgumentMethods(String name123)