Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@Async双重行为_Java_Spring - Fatal编程技术网

Java Spring@Async双重行为

Java Spring@Async双重行为,java,spring,Java,Spring,我有以下课程: @Component public class Foo { @Async public void test1() { ... } public void test2() { test1(); } } 以及另一个具有Foo实例的类: @Component public class Bar { @Autowired public Foo foo; public void testMethod() { foo.test

我有以下课程:

@Component
public class Foo {

  @Async
  public void test1() {
     ...
  }

  public void test2() {
    test1();
  }
}
以及另一个具有Foo实例的类:

@Component
public class Bar {

  @Autowired
  public Foo foo;

  public void testMethod() {
    foo.test2();
    foo.test1();
  }
}

我的经验如下:如果我调用foo.test1(),它将异步运行,但是如果我调用foo.test2()-它也调用test1()-那么test1()将不会异步运行。有人能解释这种行为吗?

Async
行为要求代理类,而不能代理自调用。因此,当
test2
调用
test1
时,
@Async
注释无效

该行为在中进行了描述:

但是,一旦调用最终到达目标对象,即本例中的SimplePojo引用,它可能对自身进行的任何方法调用,例如this.bar()或this.foo(),都将针对this引用而不是代理进行调用。这具有重要意义。这意味着自调用不会导致与方法调用关联的通知有机会执行