spring3: 切面及通知实例 Aspectj的aop
1.前置通知
接口:
package chapter1.server;
public interface IHelloService {
public void sayAdvisorBefore(String param) ;
}
实现
package chapter1.service.impl;
import chapter1.server.IHelloService;
public class HelloService implements IHelloService {
public void sayAdvisorBefore(String param) {
// TODO Auto-generated method stub
System.out.println("============say " + param);
}
}
配置:
<!-- 启动对Aspectj的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService" />
<bean id="aspect" class="chapter1.aop.HelloAspect"/>
aop:
package chapter1.aop; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class HelloAspect { //定义切入点
@Pointcut(value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) && args(param)", argNames = "param")
public void beforePointcut(String param) {} //定义通知
@Before(value = "beforePointcut(param)", argNames = "param")
public void beforeAdvice(String param) {
System.out.println("===========before advice param:" + param);
}
}
测试程序:
//前置通知
public void testAspectj()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj.xml");
IHelloService hello = context.getBean("helloService", IHelloService.class);
hello.sayAdvisorBefore("before");
}
2.后置返回通知
接口
package chapter1.server;
public interface IHelloService2 {
public int sayAfterReturning(String param);
}
实现
package chapter1.service.impl;
import chapter1.server.IHelloService2;
public class HelloService2 implements IHelloService2 {
public int sayAfterReturning(String param) {
// TODO Auto-generated method stub
System.out.println("============ say after returning:" + param);
return 1;
}
}
配置:
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService2" />
<bean id="aspect" class="chapter1.aop.HelloAspect2"/>
aop:
package chapter1.aop; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.AfterReturning; @Aspect
public class HelloAspect2 { //方法一
//通知
@AfterReturning(
//value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) and args(param)",
value="execution(* chapter1..*.sayAfterReturning(..))",
argNames="retVal",
returning="retVal")
public void afterReturningAdvice(Object retVal)
{
System.out.println("================= return after advice : " + retVal);
} //方法二
//定义切入点
@Pointcut(value="execution(* chapter1..*.sayAfterReturning(java.lang.String) and args(param))", argNames="param")
public void returnPointcut(String param) {} public void afterReturningAdvice2(Object retVal)
{ } }
测试程序:
//后置返回通知
public void testAspectAfterReturning()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj2.xml");
IHelloService2 hello = context.getBean("helloService", IHelloService2.class);
hello.sayAfterReturning("hahah");
}
3.后置错误通知
接口
package chapter1.server;
public interface IHelloService3 {
public boolean sayAfterThrow(String param);
}
实现:
package chapter1.service.impl;
import chapter1.server.IHelloService3;
public class HelloService3 implements IHelloService3 {
public boolean sayAfterThrow(String param) {
// TODO Auto-generated method stub
System.out.println("=========say after throw:" + param);
throw new RuntimeException();
}
}
配置:
<!-- 开启对Aspectj的支持 -->
<aop:aspectj-autoproxy />
<bean id="helloService" class="chapter1.service.impl.HelloService3" />
<bean id="aspect" class="chapter1.aop.HelloAspect3" />
aop:
package chapter1.aop; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing; @Aspect
public class HelloAspect3 { @AfterThrowing(value="execution(* chapter1..*.sayAfterThrow(java.lang.String))", argNames="exception", throwing="exception")
public void afterThrowAdvice(Exception exception)
{
System.out.println("=========after throwing advice : " + exception);
}
}
测试程序:
//后置错误通知
public void testAfterThrow()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj3.xml");
IHelloService3 hello = context.getBean("helloService", IHelloService3.class);
hello.sayAfterThrow("error");
}
4.环绕通知
接口:
package chapter1.server;
public interface IHelloService4 {
public void sayAround(String param);
}
实现:
package chapter1.service.impl;
import chapter1.server.IHelloService4;
public class HelloService4 implements IHelloService4 {
public void sayAround(String param) {
// TODO Auto-generated method stub
System.out.println("============= say around: " + param);
}
}
配置:
<!-- 开启对Aspectj的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService4" />
<bean id="aspect" class="chapter1.aop.HelloAspect4"/>
aop:
package chapter1.aop; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around; @Aspect
public class HelloAspect4 { @Around(value="execution(* chapter1..*.sayAround(java.lang.String))", argNames="param")
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("========= around before advice");
Object retVal = pjp.proceed(new Object[] {"我被替换了呀"});
System.out.println("========= around before advice");
return retVal; }
}
测试程序:
//环绕通知
public void testAround()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj4.xml");
IHelloService4 hello = context.getBean("helloService", IHelloService4.class);
hello.sayAround("gaga ya xia ba");
}
5.引入(结合chatper1.service.IHelloService程序来试验)
接口:
package chapter1.server;
public interface IHelloService5 {
public void sayDeclare();
}
实现:
package chapter1.service.impl;
import chapter1.server.IHelloService5;
public class HelloService5 implements IHelloService5 {
public void sayDeclare() {
// TODO Auto-generated method stub
System.out.println("=========== say declare " );
}
}
配置:
<!-- 开启对Aspect的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService"/>
<bean id="aspect" class="chapter1.aop.HelloAspect5"/>
aop:
package chapter1.aop; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents; import chapter1.server.IHelloService5;
import chapter1.service.impl.HelloService5; @Aspect
public class HelloAspect5 { @DeclareParents(
value="chapter1..*.HelloService+",
defaultImpl=HelloService5.class)
public IHelloService5 ihelloService5; }
测试程序:
//引入
@Test
public void testDeclare()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj5.xml");
IHelloService5 hello = context.getBean("helloService", IHelloService5.class);
hello.sayDeclare();
}
spring3: 切面及通知实例 Aspectj的aop的更多相关文章
- Spring AOP前置通知实例讲解与AOP详细解析
一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Service.他们都有sayHello():我们的需求是在调用这两个方法之前,要先完成写日志的 ...
- Spring使用AspectJ开发AOP:基于XML
基于XML的声明式 基于 XML 的声明式是指通过 Spring 配置文件的方式定义切面.切入点及声明通知,而所有的切面和通知都必须定义在 <aop:config> 元素中. 下面通过案例 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation
AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...
- (转)实例简述Spring AOP之间对AspectJ语法的支持(转)
Spring的AOP可以通过对@AspectJ注解的支持和在XML中配置来实现,本文通过实例简述如何在Spring中使用AspectJ.一:使用AspectJ注解:1,启用对AspectJ的支持:通过 ...
- spring3: schema的aop与Aspectj的aop的区别
schema的aop如下: 接口: package chapter6.service; public interface IHelloAroundService { public void sayAr ...
- Spring基于AspectJ的AOP的开发——注解
源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...
- Spring使用AspectJ开发AOP:基于Annotation
基于 Annotation 的声明式 在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关的配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维 ...
- AspectJ对AOP的实现
一:你应该明白的知识 1.对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且 ...
- Spring框架(6)---AspectJ实现AOP
AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...
随机推荐
- 兼容获取元素当前样式 currentStyle || getComputedStyle
function getStyle(ele, attr) { return ele.currentStyle ? ele.currentStyle[attr] : window.getComputed ...
- 流畅的python 符合python风格的对象
对象表示形式 每门面向对象的语言至少都有一种获取对象的字符串表示形式的标准方式.Python 提供了两种方式. repr() 以便于开发者理解的方式返回对象的字符串表示形式.str() 以便于用户理解 ...
- 实现num1、num2交换,无中间变量
num1=num1+num2; num2=num1-num2; num1=num1-num2;
- Dijkstra 算法初探
一.Dijkstra 算法的介绍 Dijkstra 算法,又叫迪科斯彻算法(Dijkstra),算法解决的是有向图中单个源点到其他顶点的最短路径问题.举例来说,如果图中的顶点表示城市,而边上的 ...
- github-----文件项目的推拉二式
将本地项目文件推送上线: $ git init $ git add . $ git commit -m "第一次修改" $ git log $ git remote add ori ...
- git命令与协同开发
一 git命令 1.简单命令 git init # 初始化 (建立git 版本相关文件关系都放这里) git config --global user.email "you@example. ...
- Codeforces Round #305 (Div. 2)
C. Mike and Frog 题意:有一只青蛙和一朵花,分别高度为h1.h2,每浇一次水,h1=(x1*h1+y1)mod m,h2=(x2*h2+y2)mod m.求最少浇多少次后h1=a1,h ...
- sqlserver导入excel的电话号码(身份证)变为科学计数解决方式
如果excel中有一列存的是手机号码或者身份证号码,那么导入到sql中时,会把手机或者身份证当作数字格式对待,因而会以科学记数法的形式存在sqlserver表中,解决方式,先将excel文件另存为文本 ...
- JavaScript:确认对话框
<script type="text/javascript"> function Check() { if (window.confirm('您是否参与抽奖?')) { ...
- 113. Path Sum II(求等于某个数的所有路径)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...