前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring详解篇之 AOP面向切面编程

Spring详解篇之 AOP面向切面编程

作者头像
方志朋
发布2017-12-29 15:40:26
4850
发布2017-12-29 15:40:26
举报
文章被收录于专栏:史上最简单的Spring Cloud教程

一、概述

Aop(aspect oriented programming面向切面编程),是spring框架的另一个特征。AOP包括切面、连接点、通知(advice)、切入点(pointCut) 。

1.aop几个概念:

  • 横切关注点: 对哪些方面进行拦截,拦截后怎么处理。
  • 切面(aspect):切面是横切关注点的抽象。
  • 连接点(joinpoint):被拦截的方法
  • 切入点(pointcut):对连接点进行拦截的定义。
  • 通知(advice):拦截到连接点之后要执行的代码
  • 目标对象:代理的目标对象
  • 织入
  • 引入

2.主要功能:

  • 日志记录
  • 性能统计
  • 安全控制
  • 事物处理
  • 异常处理

3.advice类型:

  • 前置通知(before advice)
  • 返回后通知(after returning advice)
  • 抛出异常后通知(after throwing advice)
  • 后通知(after advice)
  • 环绕通知(around advice)

4.Spring对AOP的支持

Spring中AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。因此,AOP代理可以直接使用容器中的其它bean实例作为目标,这种关系可由IOC容器的依赖注入提供。

二、基于xml配置的aop

在spring基于schemel中,aop需要声明一个切面aspect,一个pointcut,一个advisor. 举个例子:

切面:

“` public class MoocAspect {

代码语言:javascript
复制
public void before() {
    System.out.println("MoocAspect before.");
}

}

“`

切点:

“` public class AspectBiz {

代码语言:javascript
复制
public void biz() {
    System.out.println("AspectBiz biz.");

// throw new RuntimeException(); }

“`

配置文件:

“`

三、基于spring api方式配置aop

直接上代码:

接口中有两个方法,一个基于aop,会被拦截 ,另外一个不会被拦截。

代码语言:javascript
复制
public interface IAopService {

    public void withAop() throws Exception;

    public void withoutAop() throws Exception;

}

实现类:

代码语言:javascript
复制
public class AopServiceImpl implements IAopService {

    private String name="forezp";

    public void withAop() throws Exception {

        System.out.println("with aop run: " + name);

        if (name.trim().length() == 0)
            throw new AccountException("name cannot be null");
    }

    public void withoutAop() throws Exception {
        System.out.println("without aop");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

方法前拦截器,实现MethodBeforeAdvice,在制定方法前会被调用。

代码语言:javascript
复制
public class MethodBeforeInterceptor implements MethodBeforeAdvice {

    public void before(Method method, Object[] args, Object instance)
            throws Throwable {

        System.out.println("method invoke:" + method.getName());

        if (instance instanceof AopServiceImpl) {

            String name = ((AopServiceImpl) instance).getName();

            if (name == null)
                throw new NullPointerException("name cannot be null");
        }

    }

}

返回后拦截器,执行指定方法后会被调用。

代码语言:javascript
复制
public class MethodAfterInterceptor implements AfterReturningAdvice {

    public void afterReturning(Object value, Method method, Object[] args,
            Object instance) throws Throwable {

        System.out.println("method  " + method.getName() + "had finished and return value-" + value);

    }

}

异常拦截器,当出现异常时拦截:

代码语言:javascript
复制
public class ThrowsInterceptor implements ThrowsAdvice {

    public void afterThrowing(Method method, Object[] args, Object instance,
            AccountException ex) throws Throwable {

        System.out.println("method" + method.getName() + " throws exception:" + ex);
    }

    public void afterThrowing(NullPointerException ex) throws Throwable {

        System.out.println("the exception:" + ex);
    }

}

三个拦截器和Servce实现类需要配置到spring中。实际上,spring无法组装,需要借助代理类,把拦截器安装到NameMatchMethodPointcutAdvisor中,把自定义的bean安装到ProxyFactoryBean中,然后组装在一起:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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-2.0.xsd">

    <!-- 拦截器 在 withAop() 方法前运行 -->
    <bean id="aopMethodBeforeInterceptor"
        class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice">
            <bean
                class="com.imooc.aop.example2.MethodBeforeInterceptor" />
        </property>
        <property name="mappedName" value="withAop"></property>
    </bean>

    <!-- 拦截器 在 withAop() 返回后运行 -->
    <bean id="aopMethodAfterInterceptor"
        class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice">
            <bean
                class="com.imooc.aop.example2.MethodAfterInterceptor" />
        </property>
        <property name="mappedName" value="withAop"></property>
    </bean>

    <!-- 拦截器 在异常抛出后运行 -->
    <bean id="aopThrowsInterceptor"
        class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice">
            <bean
                class="com.imooc.aop.example2.ThrowsInterceptor" />
        </property>
        <property name="mappedName" value="withAop"></property>
    </bean>

    <bean id="aopService"
        class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 拦截器 -->
        <property name="interceptorNames">
            <list>
                <value>aopMethodBeforeInterceptor</value>
                <value>aopMethodAfterInterceptor</value>
                <value>aopThrowsInterceptor</value>
            </list>
        </property>
        <!-- 被拦截的对象 -->
        <property name="target">
            <bean
                class="com.imooc.aop.example2.AopServiceImpl">

                    <property name="name" value="forezp"></property>

            </bean>
        </property>
    </bean>

</beans>

单元测试:

method invoke:withAop with aop run: forezp method withAophad finished and return value-forezp without aop

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、概述
  • 三、基于spring api方式配置aop
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档