Introduction to Spring -- 06

Keywords: Spring



1. What is AOP?

In the software industry, AOP is the abbreviation of Aspect Oriented Programming, which means: Aspect Oriented Programming, a technology to realize the unified maintenance of program functions through precompiled mode and dynamic agent during operation. AOP is the continuation of OOP, a hot spot in software development, an important content in Spring framework, and a derivative paradigm of functional programming. AOP can isolate each part of business logic, reduce the coupling between each part of business logic, improve the reusability of program, and improve the efficiency of development.                                  ------- From Baidu Encyclopedia

  2. The role of AOP in Spring

1. Declarative transactions are provided

2. Allow the user to customize the section (section oriented programming)

In aspect oriented programming, you may encounter the following terms:

  • Crosscutting concerns: methods or functions that span multiple modules of an application. That is, the part that has nothing to do with our business logic, but we need to focus on is crosscutting concerns. Such as logging, security, caching, transactions, etc

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

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

  • Target: the target object of the proxy.

  • Proxy: the generated proxy object.

  • PointCut: refers to the connection points to be intercepted.

  • Join point: the execution point that matches the pointcut.


Five methods are provided in Advice for us to use

Notification type Connection point Implementation interface
Before advice Before method org.springfraamework.aop.MethodBeforeAdvice
Post notification After method org.springfraamework.aop.AfterReturningAdvice
Around Advice Before and after method org.aopalliance.intercept.MethodInterceptor
Exception throw notification Method throws an exception org.springfraamework.aop.ThrowsAdvice
Introduction notice Add a new method attribute to the class org.springfraamework.aop.IntroductionInterceptor


3. Use of AOP

Import its dependencies and constraints before use

        <!-- -->


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""


1. Interface using AOP

1. Write a UserService interface

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();

2. Write a UserServiceImpl

public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("Added a user");

    public void delete() {
        System.out.println("A user was deleted");

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

    public void select() {
        System.out.println("A user was queried");

3. Write two pointcuts and inherit the interface of AOP

Advance notice

public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        assert o != null;
        System.out.println(o.getClass().getName() + "of" + method.getName() + "Executed");

Post notification

public class AfterLog implements AfterReturningAdvice {

    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("Yes" + method.getName() + "The returned result is:" + o);

4. Configure AOP

    <!-- bean register -->
    <bean class="com.charles.service.UserServiceImpl" id="userService"/>
    <bean class="com.charles.log.AfterLog" id="afterLog"/>
    <bean class="com.charles.log.Log" id="log"/>

    <!-- Method 1: use native API Interface -->
    <!-- to configure AOP: Import required aop Constraints of -->
        <!-- Entry point: expression: Expression (fixed format)
        execution(Location to execute *(Modification value) *(Return value) *(Class name) *(Method name) *(Parameters)) -->
        <aop:pointcut id="pointcut" expression="execution(* com.charles.service.UserServiceImpl.*(..))"/>

        <!-- Perform surround enhancement -->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>


2. Customize AOP

1. Customize an AOP class

public class DiyLog {

    public void before(){
        System.out.println("===========Before advice ===========");

    public void after(){
        System.out.println("===========Post notification===========");


2. Configure AOP

    <bean class="com.charles.log.DiyLog" id="diyLog"/>

    <!-- Party 12: custom class -->
        <aop:aspect ref="diyLog">
            <aop:pointcut id="pointcut" expression="execution(* com.charles.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>

3. Test


3. Use notes

1. Write AOP class

public class AnnovationLog {

    @Before("execution(* com.charles.service.UserServiceImpl.*(..))")
    public void beforeLog(){
        System.out.println("===========Before advice ===========");

    @After("execution(* com.charles.service.UserServiceImpl.*(..))")
    public void afterLog(){
        System.out.println("===========Post notification===========");

    @Around("execution(* com.charles.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Surround front");
        Signature signature = joinPoint.getSignature();

        System.out.println("After surround");


2. Configure AOP and enable notes

    <!-- Open annotation -->
    <bean class="com.charles.log.AnnovationLog" id="annovationLog"/>



Posted by cookiemonster4470 on Thu, 04 Nov 2021 08:36:33 -0700