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


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{

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

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

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

    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
    public void before(Method method, Object[] objects, Object o) throws Throwable {

public class LogAfter implements AfterReturningAdvice {
    //returnValue return value
    //Method called method
    //args parameter of the object of the called method
    //Target called target object
    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=""
    <!--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-->
        <!--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>
  1. test
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");

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