SpringAOP 不适用于另一个方法内的方法调用

爪哇中定义了两种方法

public void method1(){
.........
method2();
...........
}




public void method2(){
...............
...............
}

我想让 AOP 随时待命,所以, 我创建了一个类 Java,它在方法 < strong > checkAccess 中提供了方面功能
在配置文件中,我做了如下操作

<bean id="advice" class="p.AOPLogger" />
<aop:config>
<aop:pointcut id="abc" expression="execution(*p.ABC.method2(..))" />
<aop:aspect id="service" ref="advice">
<aop:before pointcut-ref="abc" method="checkAccess" />
</aop:aspect>
</aop:config>

但是当我的 method2被调用时,AOP 功能没有被调用,也就是说 CheckAccess方法没有被 AOPLogger 类调用。

我错过了什么吗?

67775 次浏览

The aspect is applied to a proxy surrounding the bean. Note that everytime you get a reference to a bean, it's not actually the class referenced in your config, but a synthetic class implementing the relevant interfaces, delegating to the actual class and adding functionality, such as your AOP.

In your above example you're calling directly on the class, whereas if that class instance is injected into another as a Spring bean, it's injected as its proxy, and hence method calls will be invoked on the proxy (and the aspects will be triggered)

If you want to achieve the above, you could split method1/method2 into separate beans, or use a non-spring-orientated AOP framework.

The Spring doc (section "Understanding AOP Proxies") details this, and a couple of workarounds (including my first suggestion above)

It is not possible what you want to achieve. An explanation is in the Spring Reference Documentation.

Spring AOP framework is "proxy" based, the documentation at Understanding AOP Proxies explains it very well.

When Spring constructs a bean that is configured with an aspect (like "ABC" in your example), it actually creates a "proxy" object that acts like the real bean. The proxy simply delegates the calls to the "real" object but by creating this indirection, the proxy gets a chance to implement the "advice". For example, your advice can log a message for each method call. In this scheme, if the method in the real object ("method1") calls other methods in the same object (say, method2), those calls happen without proxy in the picture so there is no chance for it to implement any advice.

In your example, when method1() is called, the proxy will get a chance to do what ever it is supposed to do but if method1() calls method2(), there is no aspect in the picture. However, if method2 is called from some other bean, the proxy will be able to carry out the advice.

I had the same kind of problem and I overcame by implementing Spring's ApplicationContextAware,BeanNameAware and implementing corresponding methods as below.

class ABC implements ApplicationContextAware,BeanNameAware{


@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
applicationContext=ac;
}


@Override
public void setBeanName(String beanName) {
this.beanName=beanName;
}
private ApplicationContext applicationContext;
private String beanName;
}

then I replaced this. with ((ABC) applicationContext.getBean(beanName)). while calling the methods of the same class. This ensures that calls to methods of the same class happen through the proxy only.

So method1() changes to

 public void method1(){
.........
((ABC) applicationContext.getBean(beanName)).method2();
...........
}

Hope this helps.

Update 2022:

Now I personally prefer using TransactionHandler class described here - much cleaner and flexible way.

Original answer:

It can be done by self injection usage. You can call inner method through injected instance:

@Component
public class Foo {
@Resource
private Foo foo;
    

public void method1(){
..
foo.method2();
..
}
public void method2(){
..
}
}

Since Spring 4.3 you also can do it using @Autowired.

As of 4.3, @Autowired also considers self references for injection, i.e. references back to the bean that is currently injected.

Using @Autowired it works. Instead of calling the inner method as this.method(), you can do:

@Autowired
Foo foo;

and then calling:

foo.method2();

As indicated in the Spring docs, chapter 5.6.1 Understanding AOP proxies, there is another way you can do:

public class SimplePojo implements Pojo {


public void foo() {
// this works, but... gah!
((Pojo) AopContext.currentProxy()).bar();
}


public void bar() {
// some logic...
}
}

Although the author doesn't recommand this way. Because:

This totally couples your code to Spring AOP, and it makes the class itself aware of the fact that it is being used in an AOP context, which flies in the face of AOP. It also requires some additional configuration when the proxy is being created.

You can do self-injection this way so that this class can be used outside Spring application.

@Component
public class ABC {


@Resource
private ABC self = this;


public void method1() {
self.method2();
}


public void method2() {


}


}

Annotate calls with @EnableAspectJAutoProxy(exposeProxy = true) and call the instance methods with ((Class) AopContext.currentProxy()).method();

This is strictly not recommended as it increases to coupling

I am surprised no one mentioned this but i think we can use ControlFlowPointcut provided by Spring.

ControlFlowPointcut looks at stacktrace and matches the pointcut only if it finds a particular method in the stacktrace. essentially pointcut is matched only when a method is called in a particular context.

In this case, we can create a pointcut like

ControlFlowPointcut cf = new ControlFlowPointcut(MyClass.class, "method1");

now, using ProxyFactory create a proxy on MyClass instance and call method1().

In above case, only method2() will be advised since it is called from method1().

Another way around is to mode the method2() to some other Class File ,and that class is annotated with @Component. Then inject that using @Autowired when you need that.This way AOP can intercept that.

Example:

You were doing this...




Class demo{
method1(){
-------
-------
method2();
-------
-------
}


method2(){
------
-----
}
}


Now if possible do this :


@Component
class NewClass{
method2(){
------
-----
}
}




Class demo{


@AutoWired
NewClass newClass;


method1(){
-------
-------
newClass.method2();
-------
-------
}


}

You can refer to this question: https://stackoverflow.com/a/30611671/7278126

It's a workaround but will do the trick, the key here is to use the same bean (proxy) to invoke 'method2' instead of this.

After a lot of research, I found the following which works like a charm. But there is always a better way with ASPECTJ WEAVING. In essence of time use a self reference.

@Autowired
private ApplicationContext applicationContext;
private <<BEAN>> self;

Please note the <> is of the same class which needs to be logged with AOP or to be used for something else.

@PostConstruct
private void init() {
self = applicationContext.getBean(MyBean.class);
}

With this change all you have to do is move the calls for

this.methodName(args) -> self.methodName(args)

Hope this helps all who want to use a solution for just performance logging in the application.

You could do this :

@Autowired // to make this bean refers to proxy class


ABC self;


public void method1(){


.........


self.method2();


...........


}




public void method2(){


...............


...............


}

AOP prevents you to wrap a function if it is in the caller is in the same class of the callee.

But you can do the below work around to achieve the above by using self-injection

public class Test {




@Autowire
private Test test;




public void method1(){
.........
test.method2();
...........
}




public void method2(){
...............
...............
}
}

notice the self-injection when I did

  @Autowire
private Test test;

and I did call the method 2 by test.method2(); instead of this.method2()

Lazy! I had the same problem. I use Autowired with Lazy and it is works.

@Component
public class Foo {


@Lazy
@Autowired
private Foo foo;
    

public void method1(){
..
foo.method2();
..
}
public void method2(){
..
}
}