Spring AOP-通知-为目标方法织入多个切面

62 篇文章 1 订阅
60 篇文章 0 订阅

AOP-通知-为目标方法织入多个切面

开发中可能会遇到为目标方法织入多个切面,比如前置。后置通知都需要

接口

package com.hk.spring.aop06;

public interface SomeService {

    public void doFirst();
    public void doSecond();
}

接口实现

package com.hk.spring.aop06;

public class SomeServiceImpl implements SomeService {

    @Override
    public void doFirst() {

        System.out.println("主业务doFirst");
    }

    @Override
    public void doSecond() {

        System.out.println("主业务doSecond");
    }

}

前置通知

kpackage com.hk.spring.aop06;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

/**
* 前置通知
* @author 浪丶荡
*
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {

//在目标方法执行之前执行
@Override
public void before(Method method, Object[] args, Object target)
        throws Throwable {
    System.out.println("前置通知执行");
}

}

后置通知

package com.hk.spring.aop06;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

/**
 *  后置通知
 * @author 浪丶荡
 *
 */
public class MyAfterReturningAdvice implements AfterReturningAdvice {

    //在目标方法执行之后执行
    @Override
    public void afterReturning(Object returnValue, Method method,
            Object[] args, Object target) throws Throwable {
        System.out.println("后置通知执行");
    }


}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean name = "someService" class="com.hk.spring.aop06.SomeServiceImpl"></bean>
    <!-- 注册前置通知 -->
    <bean name = "myMethodBeforeAdvice" class="com.hk.spring.aop06.MyMethodBeforeAdvice"></bean>
    <!-- 注册后置通知 -->
    <bean name = "myAfterReturningAdvice" class="com.hk.spring.aop06.MyAfterReturningAdvice"></bean>
    <!-- 生成代理对象 -->
    <bean name = "serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <!-- 配置代理对象的目标对象属性 (类加载器)-->
    <property name="target" ref="someService"/>
    <!-- 或者这样配置
    <property name="targetName" value="someService"/>
    -->
    <!-- 配置多个切面 (方法)-->
    <property name="interceptorNames" value="myMethodBeforeAdvice,myAfterReturningAdvice"/>
    <!-- 接口通过private boolean autodetectInterfaces = true可以被找到 -->
    </bean>

</beans>

测试

package com.hk.spring.aop06;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hk.spring.aop06.SomeService;
public class Mytest {

    @Test
    public void test1(){
        String resoure = "com/hk/spring/aop06/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(resoure);
        SomeService someService = (SomeService) ac.getBean("serviceProxy");
        someService.doFirst();
        someService.doSecond();
    }
}

结果

前置通知执行
主业务doFirst
后置通知执行
前置通知执行
主业务doSecond
后置通知执行
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值