Spring AOP 浅析

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

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

AOP是但愿可以将通用需求功能从不相关的类当中分离出来,可以使得不少类共享一个行为,一旦发生变化,没必要修改不少类,而只须要修改这个行为便可。属于横向扩展编程

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

新建一个切面类mvc

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,分别在注入的方法执行开始和执行结束时运行spa

新建一个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的地方使用:code

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

运行结果以下:xml

image

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

相关文章
相关标签/搜索