catalogue
preface:
This article mainly uses XML to configure AOP
3. Create a notification class and define notification methods
4. Create Spring core configuration file (based on xml configuration method)
preface:
This article mainly uses XML to configure AOP
1, AOP concept
1. AOP (aspect oriented programming) is a programming paradigm, which belongs to the category of software engineering and guides developers how to organize program structure
2. AOP makes up for the deficiency of OOP and carries out horizontal development based on OOP
2.1 # OP stipulates that the program development takes the class as the main model, everything is carried out around the object, and the model is built before completing a task
2.2 AOP program development mainly focuses on the common functions in OOP development. Everything is carried out around the common functions. To complete a task, first build all the common functions that may be encountered (when all functions are developed, there is no difference between common and non common functions)
2, Advantages of AOP
-
Improve code reusability
-
More concise business code
-
More efficient business code maintenance
-
l more convenient business function expansion
3, Use steps
1. Import dependency
<dependencies> <!--Spring Context core package--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.2.RELEASE</version> </dependency> <!--AOP Implementation package for--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> </dependencies>
2. Create the target interface, create the implementation class, and define the target method (connection point)
The code is as follows (example):
/** * @Author:Yan * @Date 2022/01/21 17:21 * @Description Target interface **/ public interface Target { String method(String name); }
/** * @Author:Yan * @Date 2022/01/21 17:22 * @Description Target interface implementation class **/ public class TargetImpl implements Target { //Target methods that need to be enhanced @Override public String method(String name) { System.out.println("TargetImpl..."+name); return name; } }
3. Create a notification class and define notification methods
public class MyAdvice { //Before advice public void before() { System.out.println("Pre enhancement-->Print log"); } //Final notice public void after() { System.out.println("Final notice-->Print log"); } //Notify on return public void afterRun() { System.out.println("Notify on return-->Print log"); } //After throwing advice public void afterThrow() { System.out.println("After throwing advice -->Print log"); } /** * The surround notification parameter needs to be passed into the proceedingjoinpoint (provided by Spring and not created by itself) * You must pass in the ProceedingJoinPoint, otherwise it is no different from the pre notification * ProceedingJoinPoint Method of * 1,public Object proceed(Object[] args) throws Throwable Used to call the target method * 2,Object[] getArgs(); It is used to obtain the parameters of the target method and modify the parameters of the target method * @param pjp * @return * @throws Throwable */ public Object around(ProceedingJoinPoint pjp) throws Throwable { Object proceed = null; //Before advice before(); try { //Get parameters pjp.getArgs()[0] = "Li Si"; System.out.println("Around Advice -->Print log"); //Call the proceed() method, and the target method will be called here // pjp.proceed(); //Call the proceed() method and pass the set value back. In this way, you can modify the parameter value of the target method proceed = pjp.proceed(pjp.getArgs()); //Notify on return afterRun(); } catch (Throwable throwable) { //Notify in case of exception afterThrow(); throwable.printStackTrace(); } finally { //Final notice after(); } return proceed; } }
4. Create Spring core configuration file (based on xml configuration method)
<!-- Steps: 1,use bean Label creates a target implementation class object and gives it to Spring of IOC Container management 2,use bean Tag creates a notification class object and gives it to Spring of IOC Container management 3,use<aop:config>definition aop Configuration of 3.1 stay<aop:config>Inside use<aop:aspect ref="Unique identification of the notification class">Configuration section, using ref Properties, getting IOC Notification class already created in container 3.2 stay<aop:aspect>Inside use<aop:pointcut>Set entry point 3.3 Label for configuring pointcuts<aop:Notification type method="Methods in notification classes" pointcut="Pointcut expression">Multiple pointcuts can be defined --> <!-- 1,use bean Label creation MyAdvice Notice class, to Spring Administration --> <bean id="myAdvice" class="com.itheima.Advice.MyAdvice"/> <!-- 2,use bean Label creation TargetImpl The object of, give it to Spring Administration --> <bean id="target" class="com.itheima.service.impl.TargetImpl"/> <!-- 3,use<aop:com.itheima.config>Label settings AOP <aop:aspect> Set the and pointcuts for notifications --> <aop:config> <!-- Configure the pointcut expression, where the target method name is specified directly(method)--> <aop:pointcut id="po" expression="execution(* com.itheima..*.method(..))"/> <aop:aspect ref="myAdvice"> <!-- <aop:before method="before" pointcut="execution(* com.itheima..*.method(..))"/> <aop:after method="after" pointcut="execution(* com.itheima..*.method(..))"/> <aop:after-returning method="afterRun" pointcut="execution(* com.itheima..*.method(..))"/> <aop:after-throwing method="afterThrow" pointcut="execution(* com.itheima..*.method(..))"/>--> <!-- Specifies the method of the notification class --> <aop:around method="around" pointcut-ref="po"/> </aop:aspect> </aop:config>
5. Test
/** * ClassPathXmlApplicationContext Object is used to read the configuration file in xml format. You need to pass in the Spring file in xml format * <T> T getBean(Class<T> requiredType) Method, you need to pass in the target object managed by the IOC container */ @Test public void method() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Target target = context.getBean(Target.class); String s = target.method("ha-ha"); System.out.println(s); }
6. Test results
Pre enhancement-->Print log Around Advice -->Print log TargetImpl...Li Si Notify on return-->Print log Final notice-->Print log Li Si
matters needing attention:
- The surround notification method must pass in the formal parameter of the ProceedingJoinPoint object, and the target method needs to be called, otherwise the target method will not be executed
- Application.xml file
ref = unique identification of the notification class (the notification class must have been managed by the IOC container)<aop:aspect ref="myAdvice"> Where < AOP: notification type > <aop:around method="around" pointcut-ref="po"/> </aop:aspect> When modifying the notification method, remember to modify the notification type, otherwise the desired effect may not be achieved