spring AOP 注解
来源:互联网 发布:网络教育本科能考研吗 编辑:程序博客网 时间:2024/06/07 09:24
使用Spring AOP 的@AspectJ记录日志Spring AOP中的三个概念
- advice,向程序内部注入的代码
- pointcut,注入advice的位置,切入点
- advisor,将advice注入预定位置的代码
@AspectJ是基于注解的,所以需要JDK5.0以上的支持
一、最简单的打印一些东西出来
步骤:
- 创建一个Aspect类
- 配置Spring的配置文件
Aspect类中内容如下:
package com.demo.aop.log;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LogAspect {
@Before(value = "execution(public * com.demo.service..*.*(..))" )
public void beforeShow() {
System. out.println("before show." );
}
@After(value = "execution(public * com.demo.service..*.*(..))" )
public void afterShow() {
System. out.println("after show." );
}
}
解释:
在LogAspect的类声明上加上@Aspect注解
在LogAspect类中的方法上加上@Before注解,
(value = "execution(public * com.demo.service..*.*(..))" )
上面括号指定了所监视的类中的方法,上面代码中监视了com.demo.service包中的所有类的所有方法
application.xml中内容如下:
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<aop:aspectj-autoproxy />
<bean class="com.demo.aop.log.LogAspect" />
</beans>
解释:
<bean /> 节点指定了LogAspect.java的路径
二、将Pointcut和Advice分开,并将方法名和参数打印出来
步骤:
- 创建Pointcut
- 创建Advice
- 配置Spring的配置文件
LogPointcut.java中内容如下:
package com.demo.aop.log;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LogPointcut {
@Pointcut("execution(* com.demo.service..*.*(..))" )
public void inServiceLayer() { }
}
解释:
加上@Aspect注解
@Pointcut是切入点,指定需要注入代码的位置,如上面代码中指定在com.demo.service包下的所有类的所有方法中
下面只需要定义一个方法签名
LogAdvice.java中的内容如下:
package com.demo.aop.log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAdvice {
/**
* 在方法开始前纪录
* @param jp
*/
@Before("com.demo.aop.log.LogPointcut.inServiceLayer()" )
public void logInfo(JoinPoint jp) {
String className = jp.getThis().toString();
String methodName = jp.getSignature().getName(); //获得方法名
System. out.println("=====================================" );
System. out.println("====位于:" +className);
System. out.println("====调用" +methodName+"方法-开始!");
Object[] args = jp.getArgs(); //获得参数列表
if(args.length <=0){
System. out.println("====" +methodName+"方法没有参数");
} else{
for(int i=0; i<args.length; i++){
System. out.println("====参数 " +(i+1)+":"+args[i]);
}
}
System. out.println("=====================================" );
}
/**
* 在方法结束后纪录
* @param jp
*/
@After("com.demo.aop.log.LogPointcut.inServiceLayer()" )
public void logInfoAfter(JoinPoint jp) {
System. out.println("=====================================" );
System. out.println("====" +jp.getSignature().getName()+"方法-结束!");
System. out.println("=====================================" );
}
}
解释:
@Before 使用Pointcut中的方法签名找到切入点
记录并打印日志
application.xml配置文件中加入如下配置:
<aop:aspectj-autoproxy />
<bean class="com.demo.aop.log.LogAspect" />
1 0
- spring(AOP) 注解实现aop
- spring注解实现AOP
- Spring Aop 注解
- spring aop 注解
- spring aop注解Demo
- spring中 aop注解
- Spring AOP 完全注解
- Spring AOP注解配置
- Spring-AOP注解
- spring aop 注解方式
- Spring AOP 注解配置
- Spring中AOP注解
- Spring注解实现AOP
- spring aop 基于注解
- Spring aop基于注解
- spring aop 注解入门
- Spring---AOP(注解配置)
- Spring 注解Aop实现
- android开发游记:使用sharepreference存储复杂对象的解决方案
- linux中的find命令——查找文件名
- substr字符串截取时出现乱码
- Rest服务实现返回List<Map>结果
- MySQL忘记密码怎么办?
- spring AOP 注解
- 树莓派源代码安装opencv
- JS通过get、post向jsp传递中文出现乱码的问题的解决
- SDL入门教程(四):2、SDL动画的硬件渲染(Hardware Render)
- 大数据视频学习目录
- Robot Framework自动化测试修炼宝典——互动出版网
- Android性能优化典范C2
- 集合_java集合框架
- 自己搭建ngrok服务