zoukankan      html  css  js  c++  java
  • Spring框架之AOP的基本配置

    前言

    我们在前面学习了动态代理机制,Spring的AOP正是应用此机制实现的,下面我们来学习如何来配置Spring AOP,实现基本的功能。

    什么是AOP

    AOP(Aspect Oriented Programming)面向切面编程,是OOP(面向对象编程)的重要补充,面向对象是Java编程的基础,以封装、继承、多态建立了对象间的层次关系,关注的是每个对象的具体实现。面向对象允许我们开发纵向的关系。

    AOP关注横向关系,也就是说类和类之间不需要特定的关系,它能够将某些通用的操作(如:日志处理、安全验证、事务处理、异常处理、性能统计等)插入到这些无关的类中,从而可以让每个类只关注自己的核心逻辑,不必处理这些通用的操作,这个过程就是横切,而这些通用的操作就是切面Aspect。

    简单来说:AOP能把和类的核心业务无关,多个类又共同需要的逻辑代码封装起来,当对象需要时自动调用,这样就减少了类中的重复代码,降低了类之间的耦合性,提高了程序的维护性。

    AOP术语

    1、横切关注点

    对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

    2、切面(aspect)

    类是对物体特征的抽象,切面就是对横切关注点的抽象

    3、连接点(joinpoint)

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

    4、切入点(pointcut)

    对连接点进行拦截的定义

    5、通知(advice)

    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

    6、目标对象

    代理的目标对象

    7、织入(weave)

    将切面应用到目标对象并导致代理对象创建的过程

    8、引入(introduction)

    在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

    AOP应用场景

    1)日志 ,方法执行开始和完成以及出现异常都可以用日志记录

    2)缓存 ,第一次调用查询数据库,将查询结果放入内存对象, 第二次调用, 直接从内存对象返回,不需要查询数据库

    3)事务 ,调用方法前开启事务, 调用方法后提交关闭事务

    等等

    AOP的配置

    1)导入Maven依赖

    <dependencies>

    <dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>4.12</version>

    <scope>test</scope>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>4.3.14.Release</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-core</artifactId>

    <version>4.3.14.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-beans</artifactId>

    <version>4.3.14.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context-support</artifactId>

    <version>4.3.14.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-expression</artifactId>

    <version>4.3.14.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aop</artifactId>

    <version>4.3.14.RELEASE</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->

    <dependency>

    <groupId>org.aspectj</groupId>

    <artifactId>aspectjrt</artifactId>

    <version>1.8.13</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->

    <dependency>

    <groupId>org.aspectj</groupId>

    <artifactId>aspectjweaver</artifactId>

    <version>1.8.13</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-test</artifactId>

    <version>4.3.14.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>commons-logging</groupId>

    <artifactId>commons-logging</artifactId>

    <version>1.1.2</version>

    </dependency>

    <dependency>

    <groupId>log4j</groupId>

    <artifactId>log4j</artifactId>

    <version>1.2.17</version>

    </dependency>

    </dependencies>

     

    编写Service接口和实现类,这里只完成模拟操作

    public interface AdminService {

    void saveAdmin();

    void updateAdmin();

    void deleteAdmin();

    void selectAdmin();

    }

    public class AdminServiceImpl implements AdminService {

    @Override

    public void saveAdmin() {

    System.out.println("Invoke saveAdmin");

    }

    @Override

    public void updateAdmin() {

    System.out.println("Invoke updateAdmin");

    }

    @Override

    public void deleteAdmin() {

    System.out.println("Invoke deleteAdmin");

    }

    @Override

    public void selectAdmin() {

    System.out.println("Invoke selectAdmin");

    }

    }

     

    3)编写自定义增强类,该类的方法对应AOP的5种增强通知,分别是:

    1)前置通知before :方法调用前执行

    2)后置通知after-returning:方法调用后执行,出现异常不执行

    3)后置通知after:方法调用后执行,出现异常也执行

    4)异常通知after-throwing:出现异常执行

    5)环绕通知around:方法调用前后执行

     

    /**

    * 自定义增强类

    */

    public class MyAdvise {

    public void before() throws Throwable {

    System.out.println("这是before");

    }

    public void afterReturning(){

    System.out.println("这是afterReturning");

    }

    public void after(){

    System.out.println("这是after");

    }

    public void afterThrowing() throws Throwable {

    System.out.println("这是afterThrowing");

    }

    public Object around(ProceedingJoinPoint point) throws Throwable {

    System.out.println("这是around -- 前置执行");

    Object obj = point.proceed();

    System.out.println("这是around -- 后置执行");

    return obj;

    }

    }

    4) Spring配置文件

    <?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: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="userService" class="com.qianfeng.springaop.UserServiceImpl"/>

    <bean id="adminService" class="com.qianfeng.springaop.AdminServiceImpl"/>

    <bean id="myAdvise" class="com.qianfeng.springaop.MyAdvise"/>

    <aop:config>

    <!--配置切入点

    execution中第一个*代表任意返回值类型,后面是包名,第二个*代表类名的开头,第三个*代表任意方法,(..)代表任意方法参数

    -->

    <aop:pointcut id="c" expression="execution(* com.qianfeng.springaop.*ServiceImpl.*(..))"/>

    <!--配置方面-->

    <aop:aspect ref="myAdvise">

    <aop:before method="before" pointcut-ref="c"/>

    <aop:after method="after" pointcut-ref="c"/>

    <aop:after-returning method="afterReturning" pointcut-ref="c"/>

    <aop:after-throwing method="afterThrowing" pointcut-ref="c"/>

    <aop:around method="around" pointcut-ref="c"/>

    </aop:aspect>

    </aop:config>

    </beans>

    5)运行测试

    @RunWith(SpringJUnit4ClassRunner.class)

    @ContextConfiguration("classpath:applicationContext-aop.xml")

    public class TestAOP {

    @Test

    public void test(){

    ApplicationContext app = new

    ClassPathXmlApplicationContext("applicationContext-aop.xml");

    AdminService as = (AdminService) app.getBean("adminService");

    as.deleteAdmin();

    }

    }

    可以看到执行任何Service类的方法,都会调用MyAdvise中的通知方法

    总结

    本章我们学习了Spring AOP的配置,首先需要添加通知增强类,可以添加前置、后置或环绕等通知方法来完成某些通用操作,然后再Spring的配置文件中配置切入点,最后是配置切面,将通知类中的方法和切面中的方法进行绑定,最后调用Java对象的方法时,就能看到AOP的效果。

  • 相关阅读:
    Blob格式数据处理以及DataTable问题处理
    JavaScript 与 jQuery-简记
    JFinal-学习笔记(下)
    JFinal学习笔记
    工作记录
    读书笔记——计算机科学导论
    面试经验大全
    如何在liunx系统发布项目
    面试必备
    最全面的测试用例
  • 原文地址:https://www.cnblogs.com/qfchen/p/11245957.html
Copyright © 2011-2022 走看看