Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2854484
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: Java

2012-03-11 22:57:40

首先启动对@AspectJ注解的支持(蓝色部分):
       xmlns:xsi=""
       xmlns:aop=""
       xsi:schemaLocation="
          
           ">
 
 
 

配置beans.xml文件


 

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.        xmlns:xsi=""
  4.        xmlns:context=""
  5.        xmlns:aop=""
  6.        xsi:schemaLocation="
  7.            /spring-beans-2.5.xsd
  8.             /spring-context-2.5.xsd
  9.             /spring-aop-2.5.xsd">
  10.         <aop:aspectj-autoproxy/>
  11.         <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>
  12.         <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
  13. </beans>

定义一个切面


 

点击(此处)折叠或打开

  1. package cn.itcast.service;

  2. import org.aspectj.lang.ProceedingJoinPoint;
  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.  * 切面
  12.  */
  13. @Aspect
  14. public class MyInterceptor {
  15.     @Pointcut("execution (* cn.itcast.service.impl..*(..))")
  16.     private void anyMethod() {}//声明一个切入点,看上去有点怪,声明式的,

  17.     
  18.     @Before("anyMethod() && args(name)")
  19.     public void doAccessCheck(String name) {
  20.         System.out.println("前置通知:"+ name);
  21.     }
  22.     @AfterReturning(pointcut="anyMethod()",returning="result")
  23.     public void doAfterReturning(String result) {
  24.         System.out.println("后置通知:"+ result);
  25.     }
  26.     @After("anyMethod()")
  27.     public void doAfter() {
  28.         System.out.println("最终通知");
  29.     }
  30.     @AfterThrowing(pointcut="anyMethod()",throwing="e")
  31.     public void doAfterThrowing(Exception e) {
  32.         System.out.println("例外通知:"+ e);
  33.     }
  34.     
  35.     @Around("anyMethod()")
  36.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
  37.         //if(){//判断用户是否在权限

  38.         System.out.println("进入方法");
  39.         Object result = pjp.proceed();
  40.         System.out.println("退出方法");
  41.         //}

  42.         return result;
  43.     }
  44.     
  45. }


被拦截的类


 

点击(此处)折叠或打开

  1. package cn.itcast.service.impl;

  2. import cn.itcast.service.PersonService;

  3. public class PersonServiceBean implements PersonService {

  4.     public String getPersonName(Integer id) {
  5.         System.out.println("我是getPersonName()方法");
  6.         return "xxx";
  7.     }

  8.     public void save(String name) {
  9.         //throw new RuntimeException("我爱例外");

  10.         System.out.println("我是save()方法");
  11.     }

  12.     public void update(String name, Integer id) {
  13.         System.out.println("我是update()方法");
  14.     }

  15. }

测试类


 

点击(此处)折叠或打开

  1. package junit.test;


  2. import org.junit.BeforeClass;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;

  6. import cn.itcast.service.PersonService;

  7. public class SpringAOPTest {

  8.     @BeforeClass
  9.     public static void setUpBeforeClass() throws Exception {
  10.     }

  11.     @Test public void interceptorTest(){
  12.         ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
  13.         PersonService personService = (PersonService)cxt.getBean("personService");
  14.         personService.save("xx");
  15.     }
  16. }

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
前置通知:xx
进入方法
我是save()方法
后置通知:null
最终通知
退出方法



 

 


 

 

 

阅读(1246) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~