AOP在spring中的简单实现

直接看代码

  1. package edu.hust.springProxy;
  2. //抽象角色 
  3. public interface RoleDAO { 
  4.     public void doSomething(); 
  5. }
  1. package edu.hust.springProxy;
  2. //真实角色
  3. public class RoleDAOImpl implements RoleDAO {
  4.     public void doSomething() { 
  5.         System.out.println("吃牛肉。。。。。"); 
  6.     } 
  7. }
  1. package edu.hust.springProxy;
  2. import java.lang.reflect.Method;
  3. import org.springframework.aop.MethodBeforeAdvice;
  4. public class Before implements MethodBeforeAdvice {
  5.     
  6.     //before方法定义了一个advice的实现
  7.     /*
  8.      * 参数Method method是advice开始后被执行的方法, 方法名称可以用作判断是否执行代码的条件。
  9.      * Object[] args是传给被调用的public方法的参数数组. 当需要记日志时, 参数args和method都是非常有用的信息。
  10.      * Object target是执行方法m对象的引用。
  11.      * 
  12.      * Interface:org.springframework.aop.MethodBeforeAdvice
  13.      * 这个接口只有一个方法:void before(Method method, Object[] args, Object target)throws Throwable
  14.      * 方法解释:Callback before a given method is invoked. 
  15.      * Parameters:
  16.      *  (1)method - method being invoked
  17.      *  (2)args - arguments to the method
  18.      *  (3)target - target of the method invocation. May be null.
  19.      * 
  20.      * */
  21.     public void before(Method method, Object[] args, Object target) throws Throwable {
  22.         System.out.println("做事情之前, 我想我可以做点其他事情:such as --> 判断一下权限..");
  23.     }
  24.     
  25. }
  1. package edu.hust.springProxy;
  2. import java.lang.reflect.Method;
  3. import org.springframework.aop.AfterReturningAdvice;
  4. public class After implements AfterReturningAdvice {
  5.     /*
  6.      * Interface:org.springframework.aop.AfterReturningAdvice
  7.      * 这个接口只有一个方法:void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable
  8.      * 方法解释:Callback after a given method successfully returned. 
  9.      * Parameters:
  10.      *  (1)returnValue - the value returned by the method, if any
  11.      *  (2)method - method being invoked
  12.      *  (3)args - arguments to the method
  13.      *  (4)target - target of the method invocation. May be null. 
  14.      * 
  15.      * */
  16.     
  17.     public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
  18.         System.out.println("做事情之后, 我想我还可以做点其他事情:such as --> 写个日志.."); 
  19.     }
  20.     
  21. }
  1. package edu.hust.springProxy;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Client {
  5.     public static void main(String[] args) {
  6.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  7.         RoleDAO roleDAO = (RoleDAO) ctx.getBean("proxy");
  8.         roleDAO.doSomething();
  9.     }
  10. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
  3. <beans>
  4.     <!-- spring中的bean只能对类进行申明, 而不能对接口进行申明. 在某些bean的property属性中需要引入接口的话, 可以使用value来引入 -->
  5.     <bean id="roleDAOImpl" class="edu.hust.springProxy.RoleDAOImpl"></bean>
  6.     <bean id="before" class="edu.hust.springProxy.Before"></bean>
  7.     <bean id="after" class="edu.hust.springProxy.After"></bean>
  8.     
  9.     <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  10.         <!-- 对于引入了ProxyFactoryBean类的bean:
  11.             其引入"抽象角色"的property标签的name属性值必须为proxyInterfaces; 
  12.             其引入"真实角色"的property标签的name属性值必须为target;
  13.             其引入"代理角色"的property标签的name属性值必须为interceptorNames;
  14.             至于为什么, 请看ProxyFactoryBean类的源代码 -->
  15.         
  16.         <!-- proxyInterfaces:定义proxy要实现哪些接口, 可以是一个, 也可以是多个(多个的话,要用list标签) -->
  17.         <property name="proxyInterfaces">
  18.             <value>edu.hust.springProxy.RoleDAO</value>
  19.         </property>
  20.         
  21.         <!-- target:定义被代理的对象 -->
  22.         <property name="target" ref="roleDAOImpl"></property>
  23.         
  24.         <!-- interceptorNames:定义proxy被切入了哪些通知, 可以是一个, 也可以是多个(多个的话,要用list标签) -->
  25.         <property name="interceptorNames">
  26.             <list>
  27.                 <value>before</value>
  28.                 <value>after</value>
  29.             </list>
  30.         </property>
  31.     </bean>
  32. </beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值