[Spring] AOP, Aspect实例解析
最近要用到切面来统一处理日志记录,写了个小实例练了练手:
具体实现类:
public interface PersonServer {
public void save(String name);
public void update(String name, Integer id);
public String getPersonName(Integer id);
}
import org.springframework.stereotype.Component; @Component("personServerBean")
public class PersonServerBean implements PersonServer {
public void save(String name) {
System.out.println("save方法");
}
public void update(String name, Integer id) {
System.out.println("update方法");
}
public String getPersonName(Integer id) {
System.out.println("getPersonName方法");
return "myName";
}
}
切面类:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; @Aspect
@Component
public class MyInterceptor {
@Pointcut("execution(* com.alibaba.aop.PersonServerBean.*(..))")
private void anyMethod(){} @Before("anyMethod()")
public void doAccessCheck() {
System.out.println("前置通知");
}
@After("anyMethod()")
public void after() {
System.out.println("最终结果");
}
@AfterReturning("anyMethod()")
public void doAfter() {
System.out.println("后置通知");
}
@AfterThrowing("anyMethod()")
public void doAfterThrow() {
System.out.println("例外通知");
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入环绕通知");
Object object = pjp.proceed();
System.out.println("退出方法");
return object;
}
// 顺序: before->method->after->afterReturning
}
XML文件配置:
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--<context:annotation-config />这个配置可以去掉,在aspject-autoproxy里面包含了这个配置--> <aop:aspectj-autoproxy /> <context:component-scan base-package="com...." />
<!--<import resource="classpath:tutorial-beans.xml" /> 写到其他文件里面去,引入-->
</beans>
引用jar包pom文件配置:
<aspect-version>1.8.0</aspect-version>
<spring-version>3.2.7.RELEASE</spring-version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect-version}</version>
</dependency>
执行测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; @Component("springAOPTest")
public class SpringAOPTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonServer bean = (PersonServerBean) ctx.getBean("personServerBean");
bean.save("test");
}
}
执行结果:
进入环绕通知
前置通知
save方法
退出方法
最终结果
后置通知
总结-执行顺序:around->before->around->after->afterReturning
参考连接:http://blog.csdn.net/wangpeng047/article/details/8556800
[Spring] AOP, Aspect实例解析的更多相关文章
- Spring系列(五):Spring AOP源码解析
一.@EnableAspectJAutoProxy注解 在主配置类中添加@EnableAspectJAutoProxy注解,开启aop支持,那么@EnableAspectJAutoProxy到底做了什 ...
- Spring AOP应用实例demo
AOP(Aspect-Oriented Programming.面向方面编程).能够说是OOP(Object-OrientedPrograming.面向对象编程)的补充和完好.OOP引入封装.继承和多 ...
- Spring AOP 入门实例详解
目录 AOP概念 AOP核心概念 Spring对AOP的支持 基于Spring的AOP简单实现 基于Spring的AOP使用其他细节 AOP概念 AOP(Aspect Oriented Program ...
- Spring Aop 应用实例与设计浅析
0.代码概述 代码说明:第一章中的代码为了突出模块化拆分的必要性,所以db采用了真实操作.下面代码中dao层使用了打印日志模拟插入db的方法,方便所有人运行demo. 1.项目代码地址:https:/ ...
- Spring AOP @Aspect
spring提供了两个核心功能,一个是IoC(控制反转),另外一个便是Aop(面向切面编程),IoC有助于应用对象之间的解耦,AOP则可以实现横切关注点(如日志.安全.缓存和事务管理)与他们所影响的对 ...
- Spring AOP 源码解析
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...
- Spring AOP(aspect oriented programming) 转载
1.面向切面的基本原理 软件系统可以看成是由一组关注点组成的,其中,直接的业务关注点,是直切关注点.而为直切关注点提供服务的,就是横切关注点. 01.什么是面向切面编程 横切关注点:影响应用多处的功能 ...
- Spring AOP Aspect的简单实现(基于XML)
第一步:导包 第二步:实现类和切面类 Service("userService")public class IUserviceImpl implements IUserServic ...
- Spring学习(十五)----- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
随机推荐
- php二维数组相同id的数量相加
数组样式 $arr = array( array( , , , ), array( , , , ), array( , , , ), array( , , , ), ); 处理办法 $item=arr ...
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...
- 手持设备点击响应速度,鼠标事件与touch事件的那些事
前言 现在一直在做移动端的开发,这次将单页应用的网页内嵌入了app,于是老大反映了一个问题:app应用点击响应慢!我开始不以为然,于是拿着网页版的试了试,好像确实有一定延迟,于是开始了研究,最后选择了 ...
- [译]How to Write a Git Commit Message
原文: http://chris.beams.io/posts/git-commit/ 介绍:为什么好的commit message很重要 你浏览项目commit message的时候或多或少会有些困 ...
- 解决qq互联回调地址错误redirect uri is illegal(100010)的方法,不同于网上大部分方法
我是在用ecshop自己搭建的一个网店系统安装了第三方登录的插件,包括qq登录插件.网上大部分情况都是discuz论坛的这个回调地址出错及解决办法.所以在我出错的时候参考他们的办法并不管用.但是在后面 ...
- 《深入理解Java虚拟机》学习笔记1-内存数据区域
1.程序计数器 作用-较小的内存空间,用于存储当前线程所执行的字节码的行号 特性-每条线程有需要一个独立的程序计数器,各线程间互不影响,独立存储,称为"线程私有"的内存 ...
- Ajax详解
一:什么是Ajax AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法 ...
- SQLServer日期函数用法
--1.显示本月第一天 ,) ),)) --2.显示本月最后一天 ,),,))) ,,,)) --3.上个月的最后一天 ,,)) --4.本月的第一个星期一 , ) --5.本年的第一天 ,) --6 ...
- 在布局文件中使用Fragment的步骤
为了在Activity布局文件中使用Fragment我们需要四个步骤. 1.定义一个Activity,他继承android.support.v4.app.FragmentActivity,下面是关键代 ...
- jdk安装问题--javac不是外部命令
set java_home=C:\Program Files\Java\jdk1.6.0_26 安装JDK的根目录 set classpath=%JAVA_HOME%\lib\tools.jar; ...