Spring AOP 浅析

AOP (Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。它是一种新的方法论,它是对传统OOP编程的一种补充。

OOP是关注将需求功能划分为不同的并且相对独立,封装良好的类,并让它们有着属于自己的行为,依靠继承和多态等来定义彼此的关系,属于纵向扩展;

AOP是希望能够将通用需求功能从不相关的类当中分离出来,能够使得很多类共享一个行为,一旦发生变化,不必修改很多类,而只需要修改这个行为即可。属于横向扩展

例如:几乎所有的业务类中都会有记录日志的代码,但是记录日志的代码严格意义上讲又不属于这些对象的行为,同时也会产生大量的重复代码,此时应该考虑将记录日志的代码提炼出来,引入AOP。

新建一个切面类

package com.test.ssm.aop;

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

public class TestAspect {
 
    public void doBefore(JoinPoint jp) {
        System.out.println("TestAspect doBefore  结束执行方法 :" + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
    }

   public void doAfter(JoinPoint jp) {
        System.out.println("TestAspect doAfter  开始执行方法 :" + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
    }

}

包含两个方法,doBefore和doAfter,分别在注入的方法执行开始和执行结束时运行

新建一个aop.xml

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
                            http://www.springframework.org/schema/aop/spring-aop.xsd ">
    <aop:config>
        <aop:aspect id="TestAspect" ref="aspectBean">
            <aop:pointcut id="businessService" expression="execution(* com.test.ssm.aop.service.*.*(..))" />
            <aop:before pointcut-ref="businessService" method="doBefore"/>
            <aop:after pointcut-ref="businessService" method="doAfter"/>
        </aop:aspect>
    </aop:config>
    <bean id="aspectBean" class="com.test.ssm.aop.TestAspect" />
    <bean id="bService" class="com.test.ssm.aop.service.BServiceImpl"></bean>
</beans>

在aop.xml中,将TestAspect和BServiceImpl连个类注入到Spring

package com.test.ssm.aop.service;

public class BServiceImpl {

    public void doSomeThing(String _msg) {
        System.out.println("BServiceImpl doSomeThing msg : " + _msg);
    }
}

在需要调用BServiceImpl的地方使用:

ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml") ;
BServiceImpl ss = ctx.getBean(BServiceImpl.class);
ss.doSomeThing("dsfds");

运行结果如下:

image

可以看到,执行BServiceImpl的doSomeThing方法时,aop自动会注入改方法,并记录方法执行开始和结束的日志。

原文地址:https://www.cnblogs.com/jimmy-y/p/9268172.html