Create Aspects Using Annotations

Using annotations to create aspects is a key feature introduced in AspectJ 5. Before AspectJ 5, writing AspectJ aspects required learning an extension to the Java language, but AspectJ is annotation-oriented

The model can very easily turn any class into an aspect with a few annotations.

 

1.1AspectJ provides five annotations to define notifications,

The @Pointcut annotation can define reusable pointcuts within an @AspectJ aspect

 

In Audience, the performance() method is annotated with @Pointcut. The value set for the @Pointcut annotation is a pointcut expression, as previously set on the advice annotation. By adding the @Pointcut annotation to the performance() method, we are actually extending the pointcut expression language so that you can use performance() in any pointcut expression, otherwise you would need to use place to use that longer pointcut expression. We now replace long expressions in all advice annotations with performance().

The actual content of the performance() method doesn't matter, it should actually be empty here. In fact, the method itself is just an identifier for the @Pointcut annotation to attach.

 

Audience will just be a bean in the Spring container if you stop there. Even if an AspectJ annotation is used, it is not considered an aspect, these annotations are not resolved and no proxy is created to convert it into an aspect. If you use JavaConfig, you can enable auto-proxy functionality at the class level of the configuration class by using the EnableAspectJ-AutoProxy annotation. Program Checklist

4.3 shows how to enable automatic proxying in JavaConfig. .

 

 If you are using XML to wire beans in Spring, you need to use the <aop:aspectj-autoproxy> element in the Spring aop namespace. The XML configuration below shows how this can be done.

 

 

What we need to remember is that Spring's AspectJ auto-proxy only uses @AspectJ as a guide for creating aspects, and aspects are still proxy-based. In essence, it's still a proxy-based aspect of Spring. This is important because it means that despite using the @AspectJ annotation, we are still limited to proxy method calls. If we want to take advantage of all the power of AspectJ, we must use AspectJ at runtime and not rely on Spring to create proxy-based aspects

 

1.2 Create a wraparound notification

 

Surround notifications are the most powerful type of notifications. It enables the logic you write to completely wrap the target method to be advised. It's actually like writing both pre- .

Probably the first thing you notice about this new notification method is that it accepts ProceedingJoinPoint as a parameter. This object is required because you use it to call the notified method in the notification. Anything can be done in the notification method . When it wants to give control to the notified method, it needs to call the proceed() method of ProceedingJoinPoint.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325207654&siteId=291194637