Chapter 4: Spring AOP


4.1: Aspect-Oriented Programming
AOP is the programming idea of ​​cutting the code into the specified location of the class during runtime. Aspects can help us modularize cross-cutting concerns and realize the reuse of cross-cutting concerns. Spring implants the aspect into the specified bean during runtime, and actually inserts the aspect during the process of intercepting the method invocation.
4.2: Describe pointcuts
The definition of pointcuts in SpringAOP uses AspectJ pointcut expressions. But only some of AspectJ's pointcut expressions are supported. arg(), @arg(), this(), target(), @target(), within() $within(), @annotation and execution(). Among these pointcut indicators, execution() is used to implement the matching, and other indicators are used to define the matching pointcut.
Pointcut case:
execution(* package.package.class.method(..))
execution(* package.package.class.method(..) && within (package A.*)) //additional condition is: package A Any method under
execution(* package.package.class.method(..) && bean("beanID")) // additional condition is: match specific bean
execution(* package.package.class.method(. .) && !bean("beanID")) //additional condition is: does not match a specific bean
execution(* package.package.class.method(int) && args(property)) //pointcut
4.3 with arguments: Create Aspects Using Annotations
To use annotations to create aspects, you need to enable AspectJ's automatic proxy, and then use @After, @AfterReturning, @AfterThrowing, @Around, @Before annotations to create aspects with java classes.
Start AspectJ automatic proxy: start
in java configuration class:
@Configuration
@EnableAspectJAutoProxy //Start AspectJ automatic proxy
@ComponentScan
public class ConcertConfig(){@Bean...}
Start in xml:
<beans>
<aop:aspectJ-autoproxy > //Start AspectJ automatic proxy
</beans>
Use annotation to create aspect
package xxx;
/**
pointcut=aspect+advice
*/
@Aspect
public class Audience{
//pointcut
@PointCut("execution(* package.package. class.method(..))")
public void performance(){} //Used to carry the pointcut, the method body is not important, and then the method name can be used to call the aspect.
//Notification
@Before("performance()")
public void helloBefore(){System.out.println("Pre-notification execution")}
@AfterReturning("performance()")
public void helloAfterReturning(){System.out .println("Return to post-notification execution")
@AfterThrowing("performance()")
public void helloAfterThrowing(){System.out.println("Exception post-notification execution")}
}
Use annotations to create surround notification
package xxx;
@Aspect
public class Audience{
//
Pointcut @PointCut("execution(* package.package.class.method(..))")
public void performance(){} //用于承载切点,方法体不重要,之后使用方法名可调用切面。
//环绕通知
@Around("performance()")
public void helloAround(ProceedingJoinPoint jp){
try{
//执行前置通知
jp.proceed();
//执行后置通知
}catch(Throwable e){
//调用异常通知
}
}
}
4.4:在xml中声明切面
一般切面
<aop:config>
<aop:aspect ref = "audience"> //ref 引用了ID为audience的Bean,这个bean中定义了通知方法。
<!--切点-->
<aop:pointcut id = "performance" expression = "execution(* 包.包.类.方法(..))" />
<!--环绕通知-->
<aop:before pointcut-ref = "performance" method = "通知A" />
<aop:after-returning pointcut-ref = "performance" method = "通知B" />
<aop:after-throwing> pointcut-ref = "performance" method = "通知C'/>
</aop:aspect>
</aop:config>
环绕通知切面
<aop:config>
<aop:aspect ref = "audience">
<!--切点-->
<aop:pointcut id = "performance" expression = "execution(* 包.包.类.方法(..))" />
<!--环绕通知-->
<aop:around pointcut-ref = "performance" method = "helloAround"> //指点环绕通知方法
</aop:aspect>
</aop:config>
4.5:注入AspectJ切面
???????????????????

















Guess you like

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