与大多数技术一样, AOP 已经形成了自己的术语。描述切面的常用术语有通知(advice)、切点(pointcut)和连接点(join point)。从今天开始,我们对spring的切面编程做一个总结。她们都是你漫漫长路上,只配错过的好姑娘。

spring中的aop

spring的切面可以应用以下的5种类型的通知:

  • 前置通知(Before):在目标方法被调用之前调用通知功能;
  • 后置通知(After):在目标方法完成之后调用通知,此时不会关心方法的输出是什么;
  • 返回通知(After-returning):在目标方法成功执行之后调用通知;
  • 异常通知(After-throwing):在目标方法抛出异常后调用通知;
  • 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。

对于spring中aop的支持,主要分为@AspectJ 注解驱动的切面和基于xml的spring的配置。以下我们对这两种配置做一个简单的了解。测试的代码目录如下:

以后可能对于spring的关于aop学习,这个目录可能会添加代码。application-xml.xml是学习xml配置的,application-aop.xml是学习@Aspectj注解配置的。

springaop的xml配置方式

一、我们的application-xml.xml文件内容如下:

<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="myService" class="com.linux.huhx.business.service.MyAspectService3"/>
<bean id="myAspect" class="com.linux.huhx.aspect.XmlUserfulAspect"/> <aop:config>
<aop:aspect id="xmlAspect1" ref="myAspect">
<aop:pointcut id="businessService" expression="execution(* com.linux.huhx.business.service.MyAspectService3.*())"/> <aop:before method="beforeExecute" pointcut-ref="businessService"/>
<aop:after method="afterExecute" pointcut-ref="businessService"/>
</aop:aspect>
</aop:config>
</beans>

二、我们的切面类XmlUserfulAspect如下:

package com.linux.huhx.aspect;

/**
* @Author: huhx
* @Date: 2017-12-15 下午 12:31
* @Desc: 切面类
*/
public class XmlUserfulAspect { public void beforeExecute() {
System.out.println("before execute.");
} public void afterExecute() {
System.out.println("after execute.");
}
}

三、我们简单的定义一个应用切面的服务类

package com.linux.huhx.business.service;

/**
* @Author: huhx
* @Date: 2017-12-15 下午 12:28
*/
public class MyAspectService3 { public void serviceMethod() {
System.out.println("my aspect service1 method.");
}
}

四、在main的类中我们对上述的切面进行测试

package com.linux.huhx.main;

import com.linux.huhx.business.service.MyAspectService3;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @Author: huhx
* @Date: 2017-12-15 下午 12:32
* @Desc: 基于xml配置的aop测试主体类
*/
public class XmlServiceMain {
private ApplicationContext context = null; @Before
public void initApplicationContext() {
context = new ClassPathXmlApplicationContext("application-xml.xml");
} @Test
public void aspectServiceTest_1() {
MyAspectService3 myAspectService1 = context.getBean("myService", MyAspectService3.class);
myAspectService1.serviceMethod();
}
}

打印的结果如下:

before execute.
my aspect service1 method.
after execute.

springaop中关于注解的方式

  这里面设计的代码比较多,主要是为了测试不同的知识点。这里全部贴出代码,后续再做整理。后续我们对这两种的配置方式做一个比较细致的过程讲解。使用aspectj,我们需要在pom.xml文件中添加依赖:

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.11</version>
</dependency>

一、application-aop.xml的文件内容如下

<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"> <aop:aspectj-autoproxy/> <bean id="myService1" class="com.linux.huhx.business.service.MyAspectService1"/>
<bean id="myService2" class="com.linux.huhx.business.service.MyAspectService2"/> <!--declare an aspect-->
<bean id="myAspect" class="com.linux.huhx.aspect.NotVeryUsefulAspect"/>
<bean id="myAspect1" class="com.linux.huhx.aspect.LittleUserfulAspect"/>
</beans>

二、两个测试切面的服务类如下:

  • MyAspectService1:这里面主要测试切面的5种类型的通知。
package com.linux.huhx.business.service;

import java.io.FileNotFoundException;

/**
* @Author: huhx
* @Date: 2017-12-15 上午 10:08
* @Desc: 一个简单的测试aspect的实类
*/
public class MyAspectService1 { public void serviceMethod() {
System.out.println("myaspect service1 method.");
} public String returnServiceMethod() {
return "return huhx.";
} public String throwExceptionMethod() {
System.out.println("throw exception method.");
try {
throw new FileNotFoundException("not found class.");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "hello world";
} public String aroundServiceMethod1() {
System.out.println("around service method.");
return "huhx";
}
}
  • MyAspectService2.java:这里面测试切面传递参数。
package com.linux.huhx.business.service;

import java.util.Map;

/**
* @Author: huhx
* @Date: 2017-12-15 上午 11:17
*/
public class MyAspectService2 { public void serviceMethod_1(Map<String, String> map) {
System.out.println("service method." + map);
}
}

三、两个切面的类如下

  • NotVeryUsefulAspect:是针对于上述的MyAspectService1类的,为了测试5种通知类型。
package com.linux.huhx.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; /**
* @Author: huhx
* @Date: 2017-12-15 上午 9:51
* @Desc: 定义一个aspect
*/ @Aspect
public class NotVeryUsefulAspect {
private static final String pointCutString = "execution(* com.linux.huhx.business.service.MyAspectService1.*())"; @Before(pointCutString)
public void beforeExecute() {
System.out.println("before advice.");
} @After(pointCutString)
public void afterExecute() {
System.out.println("after advice.");
} @AfterReturning(value = pointCutString, returning = "retVal")
public void afterReturingExecute1(String retVal) {
System.out.println("after throwing " + retVal);
} @AfterThrowing(value = pointCutString, throwing = "exception")
public void afterThrowingExecute1(Throwable exception) {
System.out.println("throwing in advice show message: " + exception.getMessage());
} @Around(value = pointCutString)
public void arundExecute1(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("before around.");
System.out.println(pjp.proceed());
System.out.println("after around.");
}
}
  • LittleUserfulAspect:是对应于MyAspectService2类的,为了测试切面中参数的传递。
package com.linux.huhx.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; import java.util.Map; /**
* @Author: huhx
* @Date: 2017-12-15 上午 11:16
* @Desc: 定义一个切面
*/
@Aspect
public class LittleUserfulAspect { @Pointcut("execution(* com.linux.huhx.business.service.MyAspectService2.*(..)) && args(map,..)")
public void anyMethod(Map<String, String> map) {} @Before(value = "anyMethod(map)")
public void beforeExecute(Map map) {
System.out.println("before execute." + map);
}
}

四、我们的测试主体类ServiceMain

package com.linux.huhx.main;

import com.linux.huhx.business.service.MyAspectService1;
import com.linux.huhx.business.service.MyAspectService2;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.HashMap;
import java.util.Map; /**
* @Author: huhx
* @Date: 2017-12-15 上午 10:13
* @Desc: 测试的main类
*/
public class ServiceMain {
private ApplicationContext context = null; @Before
public void initApplicationContext() {
context = new ClassPathXmlApplicationContext("application-aop.xml");
} @Test
public void aspectServiceTest_1() {
MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);
myAspectService1.serviceMethod();
} @Test
public void aspectServiceTest_2() {
MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);
myAspectService1.returnServiceMethod();
} @Test
public void aspectServiceTest_3() {
MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);
myAspectService1.throwExceptionMethod();
} @Test
public void aspectServiceTest_4() {
MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);
myAspectService1.aroundServiceMethod1();
} /* 其它的切面实现类 */
@Test
public void aspectServiceTest_5() {
MyAspectService2 myAspectService2 = context.getBean("myService2", MyAspectService2.class);
Map<String, String> dataMap = new HashMap<>();
dataMap.put("username", "linux");
dataMap.put("password", "12345");
myAspectService2.serviceMethod_1(dataMap);
}
}

友情链接

springaop---->springaop的使用(一)的更多相关文章

  1. 梳理清楚springAOP,轻松面向切面编程

    不知道大家有没有这样的感觉,平时经常说aop,但是对aop中的一些概念还是模糊,总感觉很飘渺,今天来梳理下关于aop的知识. 一.概念 我们知道现在开发都是spring,讲的最多的也是springAO ...

  2. Spring-AOP实践 - 统计访问时间

    公司的项目有的页面超级慢,20s以上,不知道用户会不会疯掉,于是老大说这个页面要性能优化.于是,首先就要搞清楚究竟是哪一步耗时太多. 我采用spring aop来统计各个阶段的用时,其中计时器工具为S ...

  3. Spring-Aop入门

    (一)Aop术语定义 1.通知(advice) 通知定义了切面要做什么工作,即调用的方法,同时定义了什么时候做这些工作,即以下五种类型 (1)前置通知(Before) :在目标方法调用之前执行切面方法 ...

  4. 转-springAOP基于XML配置文件方式

    springAOP基于XML配置文件方式 时间 2014-03-28 20:11:12  CSDN博客 原文  http://blog.csdn.net/yantingmei/article/deta ...

  5. SpringAOP详解(转载大神的)

    AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之.翻译过来就是"面向方面编程&q ...

  6. spring-aop学习

     SpringAOP学习 author:luojie 1.  AOP中的基本概念 AOP的通用术语,并非spring java所特有.很遗憾AOP的术语不是特别的直观.但如果让Spring java来 ...

  7. SpringAOP之静态代理

    一.SpringAOP: ⒈AOP:Aspect Oriented Programming 面向切面编程, 实现的是核心业务和非核心业务之间的的分离,让核心类只做核心业务,代理类只做非核心业务.  ⒉ ...

  8. springaop实现登陆验证

    1.首先配置好springmvc和springaop 2.先写好登陆方法,通过注解写代理方法 通过代理获得登陆方法的参数方法名,然后再aop代理方法内进行登陆验证 贴出代码 package com.h ...

  9. spring-aop示例

    具体案例放在github上,主要是jar包在上面 https://github.com/guoyansi/spring-aop-example knights.xml <?xml version ...

  10. 使用SpringAop 验证方法参数是否合法

    (原文地址:http://blog.csdn.net/is_zhoufeng/article/details/7683194) 1.依赖包    aspectjweaver.jar 其中Maven的配 ...

随机推荐

  1. e587. Filling Basic Shapes

    There are two ways to fill basic shapes like lines and rectangles. The first is to use specific draw ...

  2. C语言中 Float 数据结构的存储计算

    1.了解float存储结构 float存储结构请看另一篇文章http://blog.csdn.net/whzhaochao/article/details/12885875 2.float最大值 fl ...

  3. c# mvc 获取 HtmlHelper 表达式值和时间格式化 去边框

    /// <summary> /// 返回没有边框的只读的TextBox标签 /// </summary> /// <typeparam name="TModel ...

  4. IE8不支持数组的indexOf方法

    在IE8下有个js错误,但是在其它浏览器下(Firefox, Chrome, IE9)下面都很正常.后来调试发现原因是在IE8下,js数组没有indexOf方法. 在使用indexOf方法前,执行一下 ...

  5. 转载:手把手教你做iOS推送

    手把手教你做iOS推送 http://www.cocoachina.com/industry/20130321/5862.html

  6. 每天一个linux命令:traceroute命令

    通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...

  7. mysql 解压版方法

    来自http://zhidao.baidu.com/link?url=RtXb2QKYTQ8Yd5TdTS7XHHiupzDaM19vlVBIrHTVmT7ZHi8kG3O9L6D6nnsfTGE-- ...

  8. Java编程思想学习笔记——一切都是对象

    前言 Java是基于C++的,但Java是一种更加纯粹的面向对象程序设计语言. C++和Java都是混合/杂合型语言.杂合型语言允许多种编程风格. 用引用操纵对象 每种编程语言都有自己操纵内存中元素的 ...

  9. ABBYY FineReader Pro for Mac系统要求

    ABBYY FineReader Pro for Mac作为先进的OCR图文识别软件,为各种各样的任务提供全面的解决方案,可轻松将纸质文档.PDF文件和数字文本照片转换为可编辑和可搜索的文件,替代了手 ...

  10. Cakephp中使用JavaScriptHelper来引入js文件

    页面的head部分的内容在Cakephp中主要是有htmlhelper来进行控制的,而js部分则是由JavaScripthelper来进行控制的,在controller里面设置好:var $helpe ...