码迷,mamicode.com
首页 > 其他好文 > 详细

aop注解 自定义切面的注解写法

时间:2014-07-12 00:02:48      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   文件   

spring.xml中

 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"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9                          http://www.springframework.org/schema/context
10                          http://www.springframework.org/schema/context/spring-context-3.0.xsd
11                          http://www.springframework.org/schema/tx
12                          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
13                          http://www.springframework.org/schema/aop 
14                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
15 <!--      扫描注解bean -->
16      <context:component-scan base-package="cn.us.aspect"/>
17 <!--      开启切面代理 使得spring认识 @Aspect -->
18      <aop:aspectj-autoproxy/>

 

 java文件中   无参数的

 1 package cn.us.aspect;
 2 
 3 
 4 import org.aspectj.lang.annotation.AfterReturning;
 5 import org.aspectj.lang.annotation.Aspect;
 6 import org.aspectj.lang.annotation.Before;
 7 import org.aspectj.lang.annotation.Pointcut;
 8 import org.springframework.stereotype.Component;
 9 
10 import cn.us.domain.User;
11 
12 @Component("logAspect")
13 @Aspect
14 public class LogAspect {
15     // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) and args(user)")   以下是无参数的写法
16      @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(..))")
17      public void pointcut()
18      {}
19     // @Before("pointcut(user)")
20       @Before("pointcut()")
21     // public void startRecordLog(User user)
22        public void startRecordLog()
23      {
24          System.out.println("Before log  has started");
25          System.out.println();
26      }
27 
28      @AfterReturning(value="pointcut()",returning="val")
29      public void endRecordLog(Object val)
30      {
31          System.out.println("@AfterReturning log  has end");
32          System.out.println(val);
33      }
34 }

 

 有参数的,

利用@before传递参数不成功,利用环绕通知

 1 package cn.us.aspect;
 2 
 3 
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.AfterReturning;
 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 import org.springframework.stereotype.Component;
11 
12 import cn.us.domain.User;
13 
14 @Component("logAspect")
15 @Aspect
16 public class LogAspect {
17     // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)")
18      @Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(..)) ")
19      public void pointcut()
20      {}
21 
22      @Around(value = "pointcut()")
23      public void aroundRecordLog(ProceedingJoinPoint pjp) throws Throwable
24      {
25          System.out.println("around before");
26          //获取参数
27          Object [] objs=pjp.getArgs();
28          for(Object obj :objs)
29          {
30              System.out.println(obj);
31          }
32          //返回值
33          Object oo=pjp.proceed();
34         
35          System.out.println("around after"  +oo);
36      }
37 }

 

aop注解 自定义切面的注解写法,布布扣,bubuko.com

aop注解 自定义切面的注解写法

标签:style   blog   http   java   color   文件   

原文地址:http://www.cnblogs.com/friends-wf/p/3832876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!