spring-aop通知
来源:互联网 发布:mad制作软件 编辑:程序博客网 时间:2024/05/16 12:32
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>
- Spring AOP-通知-前置通知
- Spring AOP-通知-后置通知
- Spring-AOP-通知-环绕通知
- Spring-AOP-通知-异常通知
- Spring-AOP通知
- spring AOP和通知
- Spring AOP-前置通知
- Spring AOP-后置通知
- Spring AOP 通知
- Spring AOP 各种通知
- Spring AOP 四大通知
- Spring--AOP-前置通知
- Spring--AOP-后置通知
- Spring--AOP-环绕通知
- Spring AOP-通知顺序
- Spring的AOP通知
- spring aop 环绕通知
- spring-aop通知
- libcurl库的使用
- android:layout_gravity="right"控制LinearLayout中控件居右显示失败
- ZooKeeper管理分布式环境中的数据(5)
- HDOJ 1335 Basically Speaking
- gcc attribute属性和section选项
- spring-aop通知
- [live555]rtsp直播基于live555的实现
- 机器学习的框架(一个框架解决几乎所有机器学习的问题)
- 使用jquery设置图片只可点击一次
- 设置smokeping开机启动
- java中的static关键字
- ZooKeeper机制架构(6)
- 如何使用Spring Data Solr搜索引擎进行开发
- 线程嵌套线程,线程run里面同步