aop implementation of spring framework

1. What is AOP

AOP(Aspect Oriented Programming) means: face-to-face programming, a technology to realize the only maintenance of program functions through precompiling and runtime dynamic agent. AOP is the continuation of OOP, a hot spot of software development, an important content of spring framework, and a derivative paradigm of functional programming. AOP can be used to isolate all parts of the business logic, which reduces the coupling between the parts of the business logic, improves the reusability of the program, and improves the efficiency of development.

The role of AOP in spring

  • Provides declarative transactions; allows users to customize facets

Here are the terms to understand:

  • Crosscutting concerns: methods or functions that span multiple modules of an application. That is, it has nothing to do with our business logic, but what we need to focus on is crosscutting concerns. Such as log, security, cache, transaction, etc

  • ASPECT: a special object whose crosscutting concerns are modularized. That is, it is a class.

  • Advice: the work that must be done in all aspects. That is, it is a method in a class.

  • Target: the notified object.

  • Proxy: the object created after the notification is applied to the target object.

  • PointCut: the definition of the "where" to perform the slice notification.

  • JointPoint: the execution point that matches the pointcut.

2. Using spring to implement AOP

Using AOP weaving, you need to import a dependency package

<dependency>
	   <groupId>org.aspectj</groupId>
	   <artifactId>aspectjweaver</artifactId>
	   <version>1.9.5</version>
</dependency>

2.1. aop implementation mode 1: spring API interface

  1. First, write our business interface and implementation class
public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}
public class UserServiceImpl implements UserService{

    @Override
    public void add() {
        System.out.println("Added a user");
    }

    @Override
    public void delete() {
        System.out.println("Deleted a user");
    }

    @Override
    public void update() {
        System.out.println("Modified a user");
    }

    @Override
    public void select() {
        System.out.println("A user was queried");
    }
}
  1. Write enhancement class, one pre enhancement, one post enhancement
public class LogBefore implements MethodBeforeAdvice {
    //Method: target object method to execute
    //objects: parameters
    //o: Target object
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"Executed"+method.getName()+"method");
    }
}

public class LogAfter implements AfterReturningAdvice {
    //returnValue return value
    //Method called method
    //args parameter of the object of the called method
    //Target called target object
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"Executed"+method.getName()+"The return value is:"+returnValue);
    }
}
  1. Configure spring's xml, implement aop, pay attention to import constraints
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--register Bean-->
    <bean id="userServiceImpl" class="com.chen.service.UserServiceImpl"></bean>
    <bean id="logBefore" class="com.chen.log.LogBefore"></bean>
    <bean id="logAfter" class="com.chen.log.LogAfter"></bean>

    <!--aop Implementation mode 1: use spring API Interface-->
    <!--to configure aop-->
    <aop:config>
        <!--Configuration entry point -> expression:expression, execution:Location to execute-->
        <aop:pointcut id="pointcut" expression="execution(* com.chen.service.UserServiceImpl.*(..))"/>
        <!--Perform orbit: advice-ref Execution method pointcut-ref: breakthrough point -->
        <aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"></aop:advisor>
        <aop:advisor advice-ref="logAfter" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>
  1. test
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");
        userServiceImpl.add();
    }
}

Note: dynamic proxy proxy is interface

UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");

Tags: Spring Programming xml encoding

Posted on Fri, 05 Jun 2020 21:49:15 -0700 by jmgrhm