4.1: Plane-oriented programming
AOP is the programming idea of cutting code into the specified location of a class during run time. Facets can help us modularize crosscutting concerns and enable reuse of crosscutting concerns. Spring inserts slices into the specified bean during run time, actually inserting slices through the interception method call.
4.2: Describe tangency
The definition of tangency in SPRINGAOP uses the tangent-point expression of ASPECTJ. However, only partial pointcut expressions that support ASPECTJ are supported. Arg (), @arg (), this (), Target (), @target (), Within () $within (), @annotation and Execution (). These pointcut indicators execution () are used to implement matching, and other indicators are used to qualify matching pointcuts.
Tangent case:
Execution (* package.. class. Method (..))
Execution (* package. Class.) method (..) && within (package a.*))//Additional condition: Any method under Package A is called
Execution (* package. Class. Method (..) && Bean ("Beanid"))//Additional condition: matches a specific bean
Execution (* package. Class.) method (..) &&!bean ("Beanid")//Additional condition: does not match a specific bean
Execution (* package.. class. Method (int) && args)//tangent point with parameters
4.3: Creating slices with annotations
creating slices with annotations requires automatic proxy for ASPECTJ, and then uses @after, @AfterReturning, @AfterThrowing, @Around, @Before annotations to create facets with Java classes.
Start ASPECTJ Auto Agent:
start in the Java Configuration class:
@Configuration
@EnableAspectJAutoProxy//start ASPECTJ Auto Agent
@ComponentScan
Public class Concertconfig () {@Bean ...}
start in XML:
<beans>
<aop:aspectJ-autoproxy>//start AspectJ Auto Agent
</beans>
creating slices with annotations
Package xxx;
/**
Tangent point = slice + notification
*/
@Aspect
Public class audience{
//Tangent Point
@PointCut ("Execution (* Package. Package. Class. Method (..))")
Public void Performance () {}//is used to host the tangency point, the method body is unimportant, and the method name can then be used to invoke the slice.
//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")}
}
create surround notifications with annotations
Package xxx;
@Aspect
Public class audience{
//Tangent Point
@PointCut ("Execution (* Package. Package. Class. Method (..))")
Public void Performance () {}//is used to host the tangency point, the method body is unimportant, and the method name can then be used to invoke the slice.
//Surround notification
@Around ("Performance ()")
Public void Helloaround (Proceedingjoinpoint jp) {
try{
//Perform a pre-notification
jp.proceed ();
//Perform post-notification
}catch (Throwable e) {
//Call exception notification
}
}
}
4.4: Declaring slices in XML
General Facets
<aop:config>
<aop:aspect ref = "Audience" >//ref references a bean with ID audience, which defines the notification method.
<!--Tangent -to-
<aop:pointcut id = "performance" expression = "Execution (* package. Class. Method (..))"/>
<!--surround Notifications -
<aop:before pointcut-ref = "performance" method = "Notify a"/>
<aop:after-returning pointcut-ref = "performance" method = "Notification B"/>
<aop:after-throwing> pointcut-ref = "performance" method = "Notify C '/>
</aop:aspect>
</aop:config>
Surround notification slices
<aop:config>
<aop:aspect ref = "Audience" >
<!--Tangent -to-
<aop:pointcut id = "performance" expression = "Execution (* package. Class. Method (..))"/>
<!--surround Notifications -
<aop:around pointcut-ref = "performance" method = "Helloaround" >//Steering Surround notification methods
</aop:aspect>
</aop:config>
4.5: Inject aspectj slices
?????????????????????????
Fourth chapter: Spring AOP