Android Annotation Injection AspectJ Oriented Aspect AOP Insertion Technology, Java (1)

Android Annotation Injection AspectJ Oriented Aspect AOP Insertion Technology, Java (1)


notification notes

@Before => Execute the code before the point cut
@After => Execute the code after the point cut
@AfterReturning => Execute the code after the point cut returns the content, you can encapsulate the return value of the point cut
@AfterThrowing => Execute after the point cut throws an exception
@Around => Surround, execute code before and after the pointcut

For example, a Before:

@Pointcut("execution(* 你的包名.network..*(..))")
public void linkToServer() {

}

//使用@Before,在切点前执行
@Before("linkToServer()")
public void doBefore(JoinPoint joinPoint) {
   //开始处理链接服务器后的操作
}

pointcut matching rules

+ means itself and subclasses
* means any type
.. means any length type

! Not  
&& and 
|| or

//所有AppCompatActivity类以及其子类testParams方法调用
@Pointcut("execution(public * testParams(..)) && target(androidx.appcompat.app.AppCompatActivity+) && args(args)")
public void activityTestParams(boolean args,AppCompatActivity activity) {
 
}

//MyLog注解在包com.my.aspectj开头的所有包下方法
@Pointcut("execution(@com.my.MyLog * com.my.aspectj..*(..))")
public void aspectLog(AppCompatActivity activity) {
 
}

target The object of the class where the cut point is located can only be an object, not: target(androidx.appcompat.app.AppCompatActivity+) or target(androidx.appcompat.app.*), it can only be target(activity)
with the specified class The link point can include the package name or class name, for example: within(androidx.appcompat.app.AppCompatActivity+) or within(androidx.appcompat.app.*)

Guess you like

Origin blog.csdn.net/zhangphil/article/details/129559632