© 版权声明:本文为博主原创文章,转载请注明出处

实例

1.项目结构

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.spring</groupId>
<artifactId>Spring-AOP</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-AOP Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<!-- 统一spring版本 -->
<spring.version>4.3.8.RELEASE</spring.version>
</properties> <dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.5.4</version>
</dependency>
</dependencies> <build>
<finalName>Spring-AOP</finalName>
</build> </project>

3.AspectBiz.java

package org.spring.aop.biz;

public class AspectBiz {

	/**
* 执行正常的业务方法
*/
public void biz() { System.out.println("执行了AspectBiz中的业务方法,无异常"); } /**
* 执行存在异常的业务方法
*/
public void throwingBiz() { System.out.println("执行了AspectBiz中的业务方法,存在异常");
throw new RuntimeException(); } /**
* 执行正常的含参的业务方法
*
* @param arg1
* 参数一
* @param arg2
* 参数二
*/
public void paramterBiz(String arg1, int arg2) { System.out.println("执行了AspectBiz中的业务方法,参数是arg1:" + arg1 + " arg2:" + arg2); } }

4.Aspect.java

package org.spring.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class Aspect {

	/**
* 前置通知
*/
public void before() { System.out.println("执行了Aspect切面的前置通知"); } /**
* 返回后通知,正常返回,抛出异常时不执行
*/
public void afterReturning() { System.out.println("执行了Aspect切面的正常返回后通知"); } /**
* 抛出异常后通知
*/
public void afterThrowing() { System.out.println("执行了Aspect切面的抛出异常后通知"); } /**
* 后通知,不管是否抛出异常
*/
public void after() { System.out.println("执行了Aspect切面的后通知"); } /**
* 环绕通知,环绕通知的第一个参数必须是ProceedingJoinPoint
*
* @param pjp
*/
public Object around(ProceedingJoinPoint pjp) { Object obj = null;
try {
System.out.println("Aspect切面环绕通知开始执行");
obj = pjp.proceed();
System.out.println("Aspect切面环绕通知执行结束");
} catch (Throwable e) {
e.printStackTrace();
}
return obj; } /**
* 含参环绕通知,环绕通知的第一个参数必须是ProceedingJoinPoint
*
* @param pjp
*/
public Object aroundParamter(ProceedingJoinPoint pjp, String arg1, int arg2) { Object obj = null;
try {
System.out.println("Aspect切面环绕通知开始执行");
System.out.println("参数是arg1:" + arg1 + " arg2:" + arg2);
obj = pjp.proceed();
System.out.println("Aspect切面环绕通知执行结束");
} catch (Throwable e) {
e.printStackTrace();
}
return obj; } }

5.Fit.java

package org.spring.aop;

public interface Fit {

	public void filter();

}

6.FitImpl.java

package org.spring.aop.impl;

import org.spring.aop.Fit;

public class FitImpl implements Fit {

	public void filter() {

		System.out.println("FitImpl filter.");

	}

}

7.spring-aop.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="aspectBiz" class="org.spring.aop.biz.AspectBiz"/><!-- 业务逻辑类 --> <bean id="aspect" class="org.spring.aop.aspect.Aspect"/><!-- 切面类 --> <aop:config><!-- AOP配置 -->
<aop:aspect id="aspectAOP" ref="aspect"><!-- 配置切面 -->
<!-- 配置切入点,org.spring.aop.biz.AspectBiz类中的所有方法 -->
<aop:pointcut expression="execution(* org.spring.aop.biz.AspectBiz.*(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/><!-- 前置通知 -->
<aop:after-returning method="afterReturning" pointcut-ref="pointcut"/><!-- 返回后通知 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/><!-- 抛出异常后通知 -->
<aop:after method="after" pointcut-ref="pointcut"/><!-- 后通知 -->
<aop:around method="around" pointcut-ref="pointcut"/><!-- 环绕通知 -->
<aop:around method="aroundParamter"
pointcut="execution(* org.spring.aop.biz.AspectBiz.paramterBiz(String,int))
and args(arg1,arg2)"/><!-- 带参数的环绕通知 -->
<!-- Introduction,在不修改类代码的前提下,为类添加新的父类 -->
<aop:declare-parents types-matching="org.spring.aop.biz.*(+)"
implement-interface="org.spring.aop.Fit"
default-impl="org.spring.aop.impl.FitImpl"/>
</aop:aspect>
</aop:config> </beans>

8.TestBase.java

package org.spring.aop.test;

import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils; public class TestBase { private ClassPathXmlApplicationContext context;
private String springXmlPath; /**
* 无参构造器
*/
public TestBase() { } /**
* 含参构造器,初始化spring配置文件路径
*
* @param springXmlPath
* spring配置文件路径
*/
public TestBase(String springXmlPath) { this.springXmlPath = springXmlPath; } /**
* 初始化加载spring配置文件到IOC容器中
*/
@Before
public void before() { if(StringUtils.isEmpty(springXmlPath)){
springXmlPath = "classpath:spring-*.xml";
}
context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
context.start(); } /**
* 销毁IOC容器
*/
public void after() { if(context != null){
context.destroy();
} } /**
* 根据bean ID获取bean对象
*
* @param beanId
* bean ID
* @return
*/
public Object getBean(String beanId) { return context.getBean(beanId); } }

9.TestSpringAop.java

package org.spring.aop.test;

import org.junit.Test;
import org.spring.aop.Fit;
import org.spring.aop.biz.AspectBiz; public class TestSpringAop extends TestBase { /**
* 通过构造器初始化spring配置文件路径
*/
public TestSpringAop() { super("classpath:spring-aop.xml"); } /**
* 测试正常业务逻辑
*/
@Test
public void testAspect() { AspectBiz biz = (AspectBiz) super.getBean("aspectBiz");
biz.biz(); } /**
* 测试异常业务逻辑
*/
@Test
public void testThrowAspect() { AspectBiz biz = (AspectBiz) super.getBean("aspectBiz");
biz.throwingBiz(); } /**
* 测试含参数业务逻辑
*/
@Test
public void testParamterAspect() { AspectBiz biz = (AspectBiz) super.getBean("aspectBiz");
biz.paramterBiz("测试数据", 1234); } /**
* 测试Introduction,在不修改类代码的前提下,为类添加新的方法和属性
*/
@Test
public void testIntroduction() { Fit fit = (Fit) super.getBean("aspectBiz");
fit.filter(); } }

10.效果预览

  10.1 执行testAspect方法

  10.2 执行testThrowAspect方法

  注:方法抛出异常后,并没有抛出异常后通知,而是执行了返回后通知,原因是因为配置了环绕通知,将环绕通知屏蔽后就正常了。

  10.3 执行testParamterAspect方法

  10.4 执行testIntroduction方法

参考:http://www.imooc.com/video/4419

    http://www.imooc.com/video/4420

   http://www.imooc.com/video/4421

   http://www.imooc.com/video/4422

    http://www.imooc.com/video/4440

Spring学习十四----------Spring AOP实例的更多相关文章

  1. spring学习 十四 注解AOP 通知传递参数

    我们在对切点进行增强时,不建议对切点进行任何修改,因此不加以使用@PointCut注解打在切点上,尽量只在Advice上打注解(Before,After等),如果要在通知中接受切点的参数,可以使用Jo ...

  2. Spring学习(十四)----- Spring Auto Scanning Components —— 自动扫描组件

    一.      Spring Auto Scanning Components —— 自动扫描组件 1.      Declares Components Manually——手动配置componen ...

  3. Spring 学习十四 Spring security安全

    Spring security: 我用过的安全机制:   oauth2, filter,  secured方法保护 9.2  保护web请求: 9.2.1  代理Servlet过滤器: Delegat ...

  4. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  5. Spring 学习(四)--- AOP

    问题 : AOP 解决的问题是什么 Spring AOP 的底层实现是什么 Spring AOP 和 AspectJ 的区别是什么 概述 在软件业,AOP为Aspect Oriented Progra ...

  6. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

  7. Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)

    在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...

  8. Spring学习(十五)----- Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  9. Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容

    © 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...

随机推荐

  1. 【08】node 之 fs文件

    var fs = require("fs");//fs 系统文件模块,对文件进行操作.Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如读取文件内容的函数有 ...

  2. 利用Docker搭建本地https环境的完整步骤

    利用Docker搭建本地https环境的完整步骤 这篇文章主要给大家介绍了关于如何利用Docker搭建本地https环境的完整步骤,文中通过示例代码将实现的步骤介绍的非常详细,对大家的学习或者工作具有 ...

  3. CODEVS【1025】选菜

    1025 选菜  时间限制: 1 s  空间限制: 128000 KB   题目等级 : 黄金 Gold 题目描述 Description 在小松宿舍楼下的不远处,有PK大学最不错的一个食堂——The ...

  4. 从数据库的表导出到Excel表格中【让客户端下载的Excel】

    原文发布时间为:2008-10-11 -- 来源于本人的百度文章 [由搬家工具导入] 这个例子是从gridview中导出到Excel,可以举一反三,可以直接从数据库中取值放在DataSet中,然后再从 ...

  5. Jobedin外企招聘网:6个要点不能不知道(转载)

    又到一年跳槽季,无数职场人士都在蠢蠢欲动,在工作之余,寻找更好的工作. Jobedin外企招聘网,特别提醒:年末跳槽,一定要在拿到新公司的Offer后,才能向现公司提出辞职.不然新工作没找到,却可能丢 ...

  6. struct sockaddr与struct sockaddr_in ,struct sockaddr_un的区别和联系

    在linux环境下,结构体struct sockaddr在/usr/include/linux/socket.h中定义,具体如下:typedef unsigned short sa_family_t; ...

  7. Scala不使用null 而使用Option,None,Some的好处

    刚接触Scala时就很奇怪, 为什么Java已经有null了,却偏偏还要弄出个None 后来依然我行我素在Scala里使用null, 结果就是经常被NullPointerException折磨得阴魂不 ...

  8. Java 获取指定日期的方法总结

    原文地址:http://bdcwl.blog.163.com/blog/static/765222652009104171521/ SimpleDateFormat sdf = new SimpleD ...

  9. weblogic内存快速配置

    # IF USER_MEM_ARGS the environment variable is set, use it to override ALL MEM_ARGS values USER_MEM_ ...

  10. Xamarin XAML语言教程Xamarin.Forms中改变活动指示器颜色

    Xamarin XAML语言教程Xamarin.Forms中改变活动指示器颜色 在图12.10~12.12中我们会看到在各个平台下活动指示器的颜色是不一样的.Android的活动指示器默认是深粉色的: ...