A simple example of spring AOP, springaop
AOP: For Aspect-oriented programming, it is to modularize things except the business part, such as logging. Just like adding, deleting, modifying, and querying student information, it can modularize output logs separately, programming through aspect pairs.
Learn some terms before writing instances.
Aspect: a section in the object operation process. It is a piece of code that implements additional modular functions.
Join point: a point in a certain stage of an object operation. A join point is actually an operation of an object.
Pointcut: The pointcut is the set of connection points, and the intersection of the aspect and the program is the entry point of the program. That is, the point where the aspect is injected into the program. In other words, the aspect is injected through the starting point.
Notification advice: indicates the processing logic adopted after a touch of the start point is cut. After intercepting the program at the entry point, you can use the notification to execute the aspect.
Target object: All notified objects (which can also be understood as objects to be proxies) are target objects. The target object is concerned by AOP and is ready to inject a plane into the target object at any time.
Weaving: the process of applying the cut surface function to the target object. A proxy object is created by the proxy factory. This proxy object can run the cut plane function for the target object, which is generated after the target object is woven into the cut plane.
The implementation of the instance is as follows:
1. Import the required jar packages-these are basic packages (you can see the overall structure)
2. Compile the instance class. The code for the Student class and the print file StudentPrint class that requires additional features is as follows:
1 package com. gp. service; 2 3 public class Student {4 public void addStudent () {5 System. out. println ("------ add student information -------"); 6} 7 public void updateStudent () {8 System. out. println ("------ modify student information -------"); 9} 10 public void deleteStudent () {11 System. out. println ("------ Delete student information -------"); 12} 13 public void selectStudent () {14 System. out. println ("------ querying student information -------"); 15} 16}
1 package com. gp. service; 2 3 public class StudentPrint {4 public void before () {5 System. out. println ("preparations before the student function is executed-start execution"); 6} 7 public void after () {8 System. out. println ("printing after the student function is implemented-the function has been implemented, print here"); 9} 10}
3. After the object class is complete, write the configuration file below:
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 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop.xsd" > 9 10 11 <bean id="student" class="com.gp.service.Student"/>12 <bean id="myStudent" class="com.gp.service.StudentPrint"/>13 <aop:config>14 <aop:pointcut expression="execution(* com.gp.service.Student.*(..))" id="pointcut1"/>15 <aop:aspect ref="myStudent">16 <aop:before method="before" pointcut-ref="pointcut1" />17 <aop:after-returning method="after" pointcut-ref="pointcut1"/>18 </aop:aspect>19 </aop:config>20 21 </beans>
Description: The expression indicates that all methods in the Student class are intercepted. The unique id is pointcut1, which is used as the starting point for cutting back.
4. files of the TestStudent class:
1 package com.gp.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.gp.service.Student; 7 8 public class TestStudent { 9 public static void main(String[] args) {10 ApplicationContext ct=new ClassPathXmlApplicationContext("applicationContext.xml");11 Student student=(Student)ct.getBean("student");12 student.addStudent();13 student.deleteStudent();14 student.updateStudent();15 student.selectStudent();16 }17 }
5. You can run the test. The result is as follows:
Some problems encountered during the running process,
1) I am using JDK 1.6 for JDK and spring versions. However, when spring uses 5.0, the test file fails to be compiled,
ApplicationContext ct = new ClassPathXmlApplicationContext ("applicationContext. xml ");
This row prompts an error.
2) When the jar package is not imported in full time, the aopalliance. jar package is not in the downloaded spring jar package and is not imported. If the result is run, the following error is reported:
Java. lang. NoClassDefFoundError: org/aopalliance/aop/Advice. importing the jar package aopalliance. jar cannot be solved yet. You need to import the following two packages:
Aopalliance. jar, aspectjweaver-1.7.4.jar
At this point, the simple AOP instance is complete, and the corresponding jar package link will be attached below
Https://pan.baidu.com/s/1b5IGRj4syjZoKFSjL7aRrw