spring-aop通知

aop通知前提需要的jar包

spring-aop-4.2.1.RELEASE.jar

com.springsource.org.aopalliance-1.0.0.jar


package com.abc.service;


public interface IsomeService {
  String doSome();
}



public class SomeServiceImpl implements IsomeService {


public SomeServiceImpl() {
System.out.println("构造方法");
// TODO Auto-generated constructor stub
}


public String doSome() {
System.out.println("doSome()方法");
return "dd";
}


}

package com.abc.aop;


import java.lang.reflect.Method;


import org.springframework.aop.MethodBeforeAdvice;
//前置通知
public class MyMethodBeforAdvice implements MethodBeforeAdvice {
//method:目标方法
//args:目标方法canlie
//target:目标对象
//该befor()方法在执行之前先调用
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("我先出来");

}

}

package com.abc.aop;


import java.lang.reflect.Method;


import org.springframework.aop.AfterReturningAdvice;
//后置通知   :改变其方法,改变不了其值
public class MyAferReturnAdvice implements AfterReturningAdvice {


@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置方法");
if(returnValue !=null){
returnValue =((String) returnValue).toUpperCase();

}
        System.out.println("retunValue="+returnValue);
}


}

package com.abc.aop;


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;


public class MyMethodInterrecpter implements MethodInterceptor {


@Override
public Object invoke(MethodInvocation mi) throws Throwable {
// TODO Auto-generated method stub
System.out.println("环绕通知");
Object result = mi.proceed();//目标对象的目标方法
if(result!=null){
result = ((String) result).toUpperCase();
}                                                                                                 环绕通知可以改变其值
return result;

}


}

环境配置                           注册目标对象,切面(通知),代理对象。  代理对象中红色标记得注意,代理对象中注册切面与目标对象,让他们联系

<?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.xsd">


<!-- 注册目标通知 -->
<bean id="someService" class="com.abc.service.SomeServiceImpl" />
<!-- 注册切面,通知 -->
<bean id="myAdvice" class="com.abc.aop.MyMethodBeforAdvice" />
<!-- 代理对象 -->
<bean id="someServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="someService" />
<property name="InterceptorNames" value="myAdvice" />
</bean>





<!-- 注册切面,通知 -->
<bean id="myAdvice2" class="com.abc.aop.MyAferReturnAdvice" />
<!-- 代理对象 -->
<bean id="someServiceProxy2" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="someService" />
<property name="InterceptorNames" value="myAdvice2" />
</bean>




<!-- 注册切面,通知 -->
<bean id="myAdvice3" class="com.abc.aop.MyMethodInterrecpter" />
<!-- 代理对象 -->
<bean id="someServiceProxy3" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="someService" />
<property name="InterceptorNames" value="myAdvice3" />
</bean>
</beans>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值