spring Aop 实现

本文通过一个具体的Java示例展示了Spring AOP(面向切面编程)的应用。示例中定义了一个学生服务接口及其实现类,并使用Spring AOP进行前置通知、后置通知及环绕通知的增强。此外,还实现了条件判断逻辑,防止特定名称被添加到学生列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java 代码
 
  1. package com.test;  
  2.   
  3. public interface StudentService {  
  4.       
  5.     public void addStudent(String name);  
  6.   
  7. }  
  8.   
  9. package com.test;  
  10.   
  11. public class StudentServiceImpl implements StudentService{  
  12.   
  13.     @Override  
  14.     public void addStudent(String name) {  
  15.         System.out.println("##增加一个姓名是"  + name + "学生!");          
  16.     }  
  17.   
  18. }  
  19.   
  20. package com.test;  
  21.   
  22. import java.lang.reflect.Method;  
  23.   
  24. import org.springframework.aop.MethodBeforeAdvice;  
  25. //调用前增强  
  26. public class BeforeAdvice implements MethodBeforeAdvice {  
  27.   
  28.     @Override  
  29.     public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {  
  30.         // TODO Auto-generated method stub  
  31.         System.out.println("##before -- 方法调入前的操作!");  
  32.     }  
  33.   
  34. }  
  35.   
  36. package com.test;  
  37.   
  38. import java.lang.reflect.Method;  
  39.   
  40. import org.springframework.aop.AfterReturningAdvice;  
  41. //结束之后  
  42. public class AfterAdvice implements AfterReturningAdvice {  
  43.   
  44.     @Override  
  45.     public void afterReturning(Object arg0, Method arg1, Object[] arg2,  
  46.             Object arg3) throws Throwable {  
  47.         // TODO Auto-generated method stub  
  48.         System.out.println("##after -- 方法返回之后的操作");  
  49.     }  
  50.   
  51. }  
  52.   
  53. package com.test;  
  54.   
  55. import org.aopalliance.intercept.MethodInterceptor;  
  56. import org.aopalliance.intercept.MethodInvocation;  
  57. //环绕增强  
  58. public class CompareInterceptor implements MethodInterceptor{  
  59.   
  60.     @Override  
  61.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  62.         // TODO Auto-generated method stub  
  63.         Object result = null;  
  64.         String name = invocation.getArguments()[0].toString();  
  65.         if(!name.equals("admin"))       result = invocation.proceed();  
  66.         else{  
  67.             System.out.println("admin不是学生,不能加入!");  
  68.         }  
  69.         return result;  
  70.     }  
  71.   
  72. }  
  73.   
  74. package com.test;  
  75. import org.springframework.aop.framework.ProxyFactoryBean;  
  76.   
  77. public class AopTest {  
  78.   
  79.     /** 
  80.      * @param args 
  81.      */  
  82.     public static void main(String[] args) {  
  83.         // TODO Auto-generated method stub  
  84.         StudentService service = new StudentServiceImpl();  
  85.         ProxyFactoryBean proxy = new ProxyFactoryBean();  
  86.         try {  
  87.             proxy.setProxyInterfaces(new Class[]{com.test.StudentService.class});  
  88.         } catch (ClassNotFoundException e) {  
  89.             // TODO Auto-generated catch block  
  90.             e.printStackTrace();  
  91.         }  
  92.         proxy.setProxyTargetClass(false);  
  93.         proxy.setTarget(service);  
  94.         proxy.addAdvice(new BeforeAdvice());  
  95.         proxy.addAdvice(new AfterAdvice());  
  96.         proxy.addAdvice(new CompareInterceptor());  
  97.         StudentService s = (StudentService)proxy.getObject();  
  98.         s.addStudent("test");  
  99.         System.out.println("----------------------------");  
  100.         s.addStudent("admin");  
  101.     }  
  102.   
  103. }  

##before -- 方法调入前的操作!
##增加一个姓名是test学生!
##after -- 方法返回之后的操作
----------------------------
##before -- 方法调入前的操作!
admin不是学生,不能加入!
##after -- 方法返回之后的操作

http://blogs.warwick.ac.uk/colinyates/entry/performance_of_spring/

利用 Spring 和 EHCache 缓存结果(翻译)


http://www.blogjava.net/zhenyu/archive/2006/07/05/56849.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值