Spring(3) AOP详解

AOP和IOC一样都是Spring中的核心思想
AOP 简介:面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring 框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要的功能有:日志记录,性能统计,安全控制,事务处理,异常处理。

下面来一个实例演示:
总体目录:
这里写图片描述
对应的jar包(注意,这里要导入aop对应的三个包):
这里写图片描述

StudentServiceAspect.java:

package com.java1234.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class StudentServiceAspect {
   //AOP切面,前置通知。都引入JointPoint参数
    public void doBefore(JoinPoint jp){
        System.out.println("类名:"+jp.getTarget().getClass().getName());
        System.out.println("方法名:"+jp.getSignature().getName());
        System.out.println("开始添加学生:"+jp.getArgs()[0]);
    }
    //后置通知
    public void doAfter(JoinPoint jp){
        System.out.println("类名:"+jp.getTarget().getClass().getName());
        System.out.println("方法名:"+jp.getSignature().getName());
        System.out.println("开始添加学生:"+jp.getArgs()[0]);
    }
    //环绕通知
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("添加学生前");
        Object retVal=pjp.proceed();
        System.out.println(retVal);
        System.out.println("添加学生后");
        return retVal;
    }
}

StudentService.java:

package com.java1234.service;

public interface StudentService {

    public void addStudent(String name);
}

StudentServiceImpl.java:

package com.java1234.service.impl;

import com.java1234.service.StudentService;

public class StudentServiceImpl implements StudentService{

    @Override
    public void addStudent(String name) {
        System.out.println("添加学生"+name);
    }

}

T.java:

package com.java1234.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java1234.service.StudentService;


public class T {

    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        StudentService studentService=(StudentService)ac.getBean("studentService");
        studentService.addStudent("张三");
    }
}

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
 <!-- 添加AOP对应的命名空间,这里在Spring文档中有 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 定义切面 -->
    <bean id="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean>

    <bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean>

    <!-- aop配置 -->
    <aop:config>
        <aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
            <!-- 定义切点 -->
            <aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
            <aop:before method="doBefore" pointcut-ref="businessService"/>
            <aop:after method="doAfter" pointcut-ref="businessService"/>
            <aop:after method="doAfter" pointcut-ref="businessService"/>
            <aop:around method="doAround" pointcut-ref="businessService"/>
        </aop:aspect> 
    </aop:config>
</beans>

注意:AOP对应的通知有前置通知,后置通知,环绕通知,返回通知,异常通知这五种,这里只演示前面三种,前三种比较常用。最后两种大家可以自行演示哦。
//测试结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/m0_37293461/article/details/80739847