Spring之AOP Annotation方式

业务接口

[java]  view plain copy
  1. package dao;  
  2.   
  3. public interface UserDao {  
  4.     public void addUser();  
  5. }  


接口实现

[java]  view plain copy
  1. package dao;  
  2.   
  3. public class UserDaoImpl implements UserDao {  
  4.   
  5.     @Override  
  6.     public void addUser() {  
  7.         // TODO Auto-generated method stub  
  8.         System.out.println("添加用户");  
  9.   
  10.     }  
  11.   
  12. }  


代理类

[java]  view plain copy
  1. package aspect;  
  2.   
  3. import org.aspectj.lang.annotation.After;  
  4. import org.aspectj.lang.annotation.AfterReturning;  
  5. import org.aspectj.lang.annotation.AfterThrowing;  
  6. import org.aspectj.lang.annotation.Around;  
  7. import org.aspectj.lang.annotation.Aspect;  
  8. import org.aspectj.lang.annotation.Before;  
  9. import org.aspectj.lang.annotation.Pointcut;  
  10.   
  11. @Aspect  
  12. public class UserDaoAspect { //类中开发切入点和各种通知  
  13.   
  14.       
  15.     @Pointcut("execution(* dao..*.*(..))")   //切入点    签名就是下面的方法名  
  16.     public void sayHello(){};  
  17.       
  18.     @Before("sayHello()")       //前置通知  
  19.     public void startSay(){  
  20.         System.out.println("Good Morning");  
  21.     }  
  22.     @AfterReturning("sayHello()")   //后置通知  
  23.     public void endSay(){  
  24.         System.out.println("bye bye");  
  25.     }  
  26.     @AfterThrowing("sayHello()")    //异常通知  
  27.     public void doThrows(){  
  28.         System.out.println("异常通知");  
  29.     }  
  30.     @After("sayHello()")       //后通知  
  31.     public void doFinally(){  
  32.         System.out.println("最终执行");  
  33.     }  
  34. }  


applicationContext.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.   
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  8.       
  9.     <bean id="userDao" class="dao.UserDaoImpl" />  
  10.       
  11.     <bean id="aspectj" class="aspect.UserDaoAspect"></bean>  
  12.       
  13.     <aop:aspectj-autoproxy/>  
  14. </beans>  


测试类

[java]  view plain copy
  1. package test;  
  2.   
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import dao.UserDao;  
  8. import dao.UserDaoImpl;  
  9.   
  10. public class Test {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.           
  18.         ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");  
  19.         UserDao dao=(UserDao)context.getBean("userDao");  
  20.         dao.addUser();  
  21.     }  
  22.   
  23. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值