AOP的实现

1.先做一个接口 里有 foo()方法
 package com.lily.rules;
 public interface IUserDAO {
  public void foo();
 }
2.在做一个实现接口的类
 package com.lily.rules;

 public class UserDAOImp implements IUserDAO {
 public void foo() {//固定的方法
  // TODO Auto-generated method stub
  System.out.println("hello first");
 }

 }
3.做一个测试类
 package com.lily.rules;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 public class MainApplication {
 public static void main(String args[]){
  ApplicationContext  txt = new FileSystemXmlApplicationContext  

("D:\\workspace\\FirstSample\\com\\lily\\rules\\applicationContext.xml");
  IUserDAO  log    = (IUserDAO) txt.getBean("businesslogicbean");
  log.foo();
 }
 }


 }
4.做一个Before类:固定的类名和实现的接口
 package com.lily.rules;
 import java.lang.reflect.Method;
 import org.springframework.aop.MethodBeforeAdvice;
 public class TracingBeforeAdvice implements MethodBeforeAdvice {

 public void before(Method arg0, Object[] arg1, Object arg2)
   throws Throwable {
  // TODO Auto-generated method stub
  System.out.println(" hello before");
 }

 }
5.开始做After类:固定的类名和实现的接口
 package com.lily.rules;
 import java.lang.reflect.Method;
 import org.springframework.aop.AfterReturningAdvice;
 public class TracingAfterAdvice implements AfterReturningAdvice {

 public void afterReturning(Object arg0, Method arg1, Object[] arg2,
   Object arg3) throws Throwable {

          System.out.println("hello after");

 }
6.做配置信息 applicationContext.xml文件
<beans></beans>
一个完整的切面操作代码完成了!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值