A simple example of aop in spring

Posted by mahendrakalkura on Fri, 14 Jun 2019 20:03:37 +0200

aop, or Face-Oriented Programming, aims at separating concerns. For example, a knight only needs to care for his safety or his expedition, and who records and celebrates his brilliant life? Of course, it is not himself. This can be fully celebrated by poets, such as when a knight is on the march, when a poet can send off, when a knight is braveAt the time of sacrifice, a poet can write a poem to celebrate a knight's life.So Knights just need to focus on how to fight.Poets, on the other hand, only need to focus on writing poems to celebrate and celebrate, which separates their functions.Therefore, the poet can be regarded as a facet. Before and after a knight's expedition, the poet is responsible for celebrating and writing poems (records).Moreover, this facet can be used by many knights or saints, not just one knight.This separates the focus and reduces the complexity of the code.The code example is as follows:

Knights:

 1 package com.cjh.aop2;
 2 
 3 /**
 4  * @author Caijh
 5  *
 6  * 2017 July 11, 2001 3:53:19 p.m.
 7  */
 8 public class BraveKnight {
 9     public void saying(){
10         System.out.println("I am a knight");
11     }
12 }

Poets:

 1 package com.cjh.aop2;
 2 
 3 /**
 4  * @author Caijh
 5  *
 6  * 2017 July 11, 2001 3:47:04 p.m.
 7  */
 8 public class Minstrel {
 9     public void beforSay(){
10         System.out.println("Before advice");
11     }
12     
13     public void afterSay(){
14         System.out.println("after returning advise");
15     }
16 }

spring profile:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
 7     <!-- Target object -->
 8     <bean id="knight" class="com.cjh.aop2.BraveKnight"/>
 9     <!-- section bean -->
10     <bean id="mistrel" class="com.cjh.aop2.Minstrel"/>
11     <!-- AoP -->
12     <aop:config>
13         <aop:aspect ref="mistrel">
14             <!-- Define the point of tangency -->
15             <aop:pointcut expression="execution(* *.saying(..))" id="embark"/>
16             <!-- Declaration Pre-Notification (Called before the tangent method is executed)-->
17             <aop:before method="beforSay" pointcut-ref="embark"/>
18             <!-- Post-declaration Notification (Called after the tangent method is executed)-->
19             <aop:after method="afterSay" pointcut-ref="embark"/>
20         </aop:aspect>
21     </aop:config>
22 </beans>

Test code:

 1 package com.cjh.aop2;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * @author Caijh
 8  *
 9  * 2017 July 11, 2001 4:02:04 p.m.
10  */
11 public class Test {
12     public static void main(String[] args) {
13         ApplicationContext ac = new ClassPathXmlApplicationContext("com/cjh/aop2/beans.xml");
14         BraveKnight br = (BraveKnight) ac.getBean("knight");
15         br.saying();
16     }
17 }

The results are as follows:

Before advice
I am a knight
after returning advise

=======================================================================================

The benefit of AOP (Face Oriented Programming) is that when we perform the behavior that we focus on (knight objects), that is, the point of tangency, then Face (Poet objects), automatically serves us without much attention.As in the test code above, we only call the say () method of the BraveKnight class, which executes the pre-notification method before the saying method and automatically executes the post-notification after the saying method is executed.This allows us to set permissions and log processing.

 

Supplement: The pointcut execution method is written in the following format

Project directory structure:

If nofoundclass errors occur during runtime, there are typically fewer: aspectjweaver.jar, a package that needs to be downloaded and added to the cloud disk to provide a link: http://pan.baidu.com/s/1mhGsZI4 password: y7wt

Topics: Java Spring Programming xml encoding