Sping aop面向切面编程通知的方法是什么

60次阅读
没有评论

共计 3365 个字符,预计需要花费 9 分钟才能阅读完成。

本篇内容介绍了“Sping aop 面向切面编程通知的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

建议先看看:java 静态代理和动态代理

一、创建两个接口:

接口 TestServiceInter:

package com.hubin.aop;
public interface TestServiceInter { public void sayHello();
}

接口 TestServiceInter2:

package com.hubin.aop;
public interface TestServiceInter2 { public void sayBye();
}

二、创建被代理类 Test1Service 该类实现了上面的两个接口

package com.hubin.aop;
 * 
 * 
 *  被代理类,相当于我自己干事情
 * @author Administrator
 *
 */
public class Test1Service implements TestServiceInter,TestServiceInter2 {
 private String name;
 
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public void sayHello() {
 // TODO Auto-generated method stub
 System.out.println(hi  +name);
 }
 public void sayBye() {
 // TODO Auto-generated method stub
 System.out.println(bye  +name);
 }
}

三、创建前置通知 MyMethodBeforeAdvice 类

package com.hubin.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
 *  前置通知类,类似于中介公司实现我交代的事情之前做的一些事情
 * @author Administrator
 *
 */
public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
 /**
 * method:  被调用方法名字
 * args:  给 method 传递的参数
 * target:  目标对象
 */
 public void before(Method method, Object[] args, Object target)
 throws Throwable {
 // TODO Auto-generated method stub
 System.out.println( 前置通知被调用 
 }
}

四、创建后置通知类 MyMethodAfterAdvice:

package com.hubin.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
 *  后置通知
 * @author Administrator
 *
 */
public class MyMethodAfterAdvice implements AfterReturningAdvice{
 @Override
 public void afterReturning(Object returnValue, Method method,
 Object[] args, Object target) throws Throwable {
 // TODO Auto-generated method stub
 System.out.println( 后置通知被调用 
 
 }
}

五、创建环绕通知类 MyMethodAroundAdvice:

package com.hubin.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodAroundAdvice implements MethodInterceptor {
 @Override
 public Object invoke(MethodInvocation arg0) throws Throwable {
 // TODO Auto-generated method stub
 System.out.println( 调用环绕通知前 
 arg0.proceed();// 这句话不调用的话,被代理类的方法不会运行
 System.out.println( 调用环绕通知后 
 return null;
 }

 */ public class MyMethodThrowsAdvice implements ThrowsAdvice { public void afterThrowing(Method m,Object []obj,Object target,Exception e){ System.out.println( 出事了 +e.getMessage());  } }

七、创建配置文件 beans.xml(文件位置在包 com/hubin/aop/)

?xml version= 1.0  encoding= UTF-8 ? 
 beans
 xmlns= http://www.springframework.org/schema/beans 
 xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance 
 xmlns:p= http://www.springframework.org/schema/p 
 xsi:schemaLocation= http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

!--  配置被代理的对象  -- bean id= test1Service   >

八、随便创建一个类用来做测试

package com.hubin.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
 /**
 * @param args
 */
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 ApplicationContext ac=new ClassPathXmlApplicationContext( com/hubin/aop/beans.xml 
 TestServiceInter ts=(TestServiceInter) ac.getBean( proxyFactoryBean 
 ts.sayHello();
 //((TestServiceInter2)ts).sayBye();
 
 }
}

九、运行结果:

 前置通知被调用
调用环绕通知前
hi  王大锤
调用环绕通知后
后置通知被调用
前置通知被调用
调用环绕通知前
bye  王大锤
调用环绕通知后
后置通知被调用 

“Sping aop 面向切面编程通知的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计3365字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)