Spring AOP——面向切面编程入门

一、AOP简介
1 . 面向切面编程(Aspect Oriented Programming):Spring的一个关键的组件就是 AOP框架。 尽管如此,Spring IoC容器并不依赖于AOP,这意味着可以自由选择是否使用AOP ,AOP提供强大的中间件解决方案,这使得Spring IoC容器更 加完善。Spring AOP的出现是为了取代 EJB中的事务机制,它有这种 声明式的事务机制,其实AOP的这种思想早就已经有了,并不 是一种什么新的技术,也并不是说专门由java这里来实现的。面向切面编程(AOP)提供另外一种角度来思考程序结构,通 过这种方式弥补了面向对象编程(OOP)的不足。
2 . AOP的应用范围:
• Persistence(持久化)
• Transaction management(事务管理)
• Security(安全)
• Logging,tracing,profiling and monitoring(日志,跟踪,优化,监控)
• Debugging(调试)
• Authentication(认证)
• Context passing(上下文传递)
• Error/Exception handling(错误/异常处理)
• Lazy loading(懒加载)
• Performance optimization(性能优化)
• Resource pooling(资源池)
• Synchronization(同步)
3 . AOP的优点:
• 高度模块化,使得我们的系统更易实现和更易维护
• 使每个模块承担的责任更清晰,提高代码的可追踪性
• 解决设计时两难的局面,在不需改动原先代码的情况下推迟不 必要的需求的实现
• 提高代码的重用性
• 加速系统的开发和部署,提高程序员的开发效率
• 降低系统开发的成本
二、AOP涉及到的概念
1 . Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是对横切性关注点的抽象。
2 . joinpoint(连接点):所谓连接点是指那些被拦截到的点。(简单来说类中的方法就可以看成是连接点)
3 . Pointcut(切入点):所谓切入点是指我们要对哪些joinpoint进行拦截的定义。(定义要对哪些方法进行操作)
4 . Advice(通知):所谓通知是指拦截到joinpoint之后所要做的事情,通知分为前置通知(before),后置通知(after-returning),异常通知(after-throwing),最终通知(after),环绕通知(around)。
5 . Target(目标对象):代理的目标对象。
6 . Weave(织入):指将aspects应用到target对象并导致proxy对象创建的过程。
7 . Introduction(引入):在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field。

三、基于注解方式的AOP编程实例
1 . 导入AOP的jar

    compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.6.6'
    compile group: 'org.aspectj', name: 'aspectjrt', version: '1.6.6'

2 . 配置文件 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd "
       default-autowire="byName">
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <bean id="SellerService" class="com.edusoft.dto.aop.SellerService"></bean>
    <bean class="com.edusoft.dto.aop.ServiceAdvisor"></bean>
</beans>

3 . 定义连接点 SellerService.java

package com.edusoft.dto.aop;
//放增强之前的代码
public class SellerService {
    public void add(){
        System.out.println("add");
    }
    public void findOneById(){
        System.out.println("findOneById");
    }
    public void findOneByProp(){
        System.out.println("findOneByProp");
    }
    public void findOneByName(){
        System.out.println("findOneByName");
    }
}

4 . 定义切面 SellerAdvisor.java

package com.edusoft.dto.aop;
import org.aspectj.lang.annotation.*;

@Aspect
//切面
public class ServiceAdvisor {
    //在execution中说明切入的方法有什么特征,第一说明返回类型,第二说明方法名,第三说明方法参数(不指定就用..)
    //只要执行符合要求的方法就会触发
    @Pointcut("execution(* add(..))")
    //切入点,是一个特殊方法, 没有返回值、入参、实现。
    private void addPointcut(){}

    //advice通知,写要执行的代码
    @Before(value = "addPointcut()")//填入切入点
    public void AopAdd(){
        System.out.println(this.getClass().getName()+"的增强代码:");
        System.out.println("检测通知");
        System.out.println("通知通过");
    }

    @Pointcut("execution(* find*(..))")
    private void findPiontcut(){ }

    @After(value = "findPiontcut()")
    public void AopFind(){
        System.out.println(this.getClass().getName()+"的增强代码:");
        System.out.println("日志添加");
        System.out.println("添加完成");
    }
}

5 . 测试 TestAop.java

package com.edusoft.aop;

import com.edusoft.dto.aop.SellerService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {
    ApplicationContext ac;
    @Before
    public void init(){
        ac=new ClassPathXmlApplicationContext("config/applicationContext.xml");
    }

    @Test
    public void testSA(){
        SellerService ss=ac.getBean("SellerService",SellerService.class);
        ss.add();
    }

    @Test
    public void testFind(){
        SellerService ss=ac.getBean("SellerService",SellerService.class);
        ss.findOneById();
        ss.findOneByName();
        ss.findOneByProp();
    }
}

testSA() 运行结果
这里写图片描述
testFind() 运行结果
这里写图片描述

四、学习思考
什么时候用到AOP编程呢,假设工程中不同的类中用到相同的代码,如果每个地方都写一遍这些代码那么代码将变得十分臃肿。这时我们可以将这部分代码抽取出来,再通过AOP注入到需要的地方,这样编程的效率将提高。
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值