当前位置:网站首页>On AspectJ framework
On AspectJ framework
2022-07-29 05:15:00 【It's a quack】
Catalog
AspectJ Common notification types
AspectJ The pointcut expression of ( master )
AspectJ Prior notice of @Before
Implementation of pre notification annotation
Pre notification mode parameters jionpoint analysis
AspectJ Frame switching JDK Dynamic proxy sum CGLib A dynamic proxy
AspectJ frame

AspectJ It's a faceted framework , He defined AOP Some of the syntax , There is a special bytecode generator to generate compliance java canonical class file .

AspectJ The notification types of include not only the four kinds of notifications we have learned before : Pre notice 、 The rear notice 、 Surround notification and exception notification , And there is one more Final notice That is, whether the program is normally executed , The code of the final notification will be executed .
The reason is that Pointcut expression , Pointcut expressions can Identify which classes and methods the facet is woven into , Just Unwanted Like we used before spring Realization AOP like that A variety of configurations . As long as the implementation of the section is configured , Then write the pointcut expression .
AspectJ Common notification types
AspectJ There are four types of notifications commonly used in :




1) Pre notice @Before
2) The rear notice @AfterReturning
3) Surrounding the notification @Around
4) Final notice @After
5) Defining entry points @Pointcut( understand )
AspectJ The pointcut expression of ( master )
Canonical formula :
execution( Access right Method return value Method statement ( Parameters ) Exception types )
The simplified formula :
execution( Method return value Method statement ( Parameters ) )
The symbols used :
* Code any arbitrary character ( wildcard )
.. If it appears in the parameter of the method , Represents any parameter
If it appears in the path , Represents this path and all its sub paths
Example :
execution(public * *(..)) : Any public method
execution(* set*(..)): Any one of them “set” The way to start
execution(* com.xyz.service.impl.*.*(..)): Any return value type , stay com.xyz.service.impl Any parameter of any method of any class under the package
execution(* com.xyz.service..*.*(..)): Any return value type , stay com.xyz.service And any parameter of any method of any class under its subpackage
com.xyz.service.a.b.*.*(..) com.xyz.service.*.*(..)
execution(* *..service.*.*(..)):service There can be any sub package before
execution(* *.service.*.*(..)):service There was only one bag before
AspectJ Prior notice of @Before
Cut into the facet function before the execution of the target method . The return value of the target method cannot be obtained in the facet method , Only the signature of the target method can be obtained .
Implementation steps :
Add dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
1) Creating a business interface
2) Create a business implementation
3) Create the section class , Implementation of section method
4) stay applicationContext.xml File for slice binding

Project case :
@Aspect // hand AspectJ Framework to identify facet classes
@Component
public class MyAspect {
/**
* All facet functions are realized by facet method
* Various aspects can be developed in this class
*
* Specification of aspect method of pre notification
* 1) Access rights are public
* 2) The return value of the void
* 3) Method name customization
* 4) Method has no parameters , If so, it can only be JoinPoint type
* 5) You have to use @Before Annotation to declare that the timing of entry is the pre cutting function and entry point
* Parameters :value Specify pointcut expression
*
* Business methods
* public String doSome(String name, int age)
*/
@Before(value = "execution(public String com.bjpowernode.s01.SomeServiceImpl.*(String,int))")
public void myBefore(){
System.out.println(" Implementation of pre notification function in aspect method ............");
}
@Before(value = "execution(public * com.bjpowernode.s01.SomeServiceImpl.*(..))")
public void myBefore(){
System.out.println(" Implementation of pre notification function in aspect method ............");
}
@Before(value = "execution( * com.bjpowernode.s01.*.*(..))")
public void myBefore(JoinPoint jp){
System.out.println(" Implementation of pre notification function in aspect method ............");
System.out.println(" Signature of the target method :"+jp.getSignature());
System.out.println(" Parameters of the target method :"+ Arrays.toString(jp.getArgs()));
}
@Before(value = "execution( * com.bjpowernode.s01..*(..))")
public void myBefore(){
System.out.println(" Implementation of pre notification function in aspect method ............");
}
@Before(value = "execution( * *(..))")
public void myBefore(){
System.out.println(" Implementation of pre notification function in aspect method ............");
}
}
Implementation of pre notification annotation


Pre notification mode parameters jionpoint analysis

AspectJ Frame switching JDK Dynamic proxy sum CGLib A dynamic proxy

<aop:aspectj-autoproxy ></aop:aspectj-autoproxy> ===> The default is JDK A dynamic proxy , Interface type must be used when fetching <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> ==> Set to CGLib Subclass proxy , You can use interfaces and implementation classes
remember : Use the interface to connect , Never make mistakes .

边栏推荐
- JS (in ES6) sync & await understanding
- Getting started with solidity
- Northeast University Data Science Foundation (matlab) - Notes
- Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
- 一文带你搞懂环绕通知@Around与最终通知@After的实现
- Unity metaverse (III), protobuf & socket realize multi person online
- ARFoundation从零开始8-Geospatial API(地理空间)开发
- pytorch学习笔记
- JS daily question (11)
- DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021
猜你喜欢

The difference between the two ways of thread implementation - simple summary

后置通知的流程分析与功能实现有哪些内容你还记得吗?

excel怎么设置行高和列宽?excel设置行高和列宽的方法

Solution | get the relevant information about the current employees' highest salary in each department |

ODOO开发教程之透视表

How to solve the problem of configuring the progress every time Office2010 is opened?

Functions in MySQL statements

Solve the warning prompt of MySQL mapping file

深度学习刷SOTA的一堆trick

Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
随机推荐
Functions in MySQL statements
Open the tutorial of adding and modifying automatically playing music on the open zone website
Jackson解析JSON详细教程
The difference between the two ways of thread implementation - simple summary
TMUX essays
那个准时上下班,从不愿意加班加点的人,在我前面升职了...
JS (foreach) return cannot end the function solution
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
Cache penetration, cache breakdown, cache avalanche and Solutions
开源汇智创未来 | 2022开放原子全球开源峰会 openEuler 分论坛圆满召开
NumPy基础
ODOO开发教程之透视表
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
自贸经济中架起的“隐形桥梁”:国货精品与中国AI力量
JS daily question (11)
scikit-learn——机器学习应用开发的步骤和理解
Original code, inverse code, complement code
The method and detailed code of automatically pop-up and QQ group when players visit the website
About realizing page Jump of website in Servlet
ARFoundation入门教程10-平面检测和放置