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要方便的 ...
随机推荐
- 如何理解Nginx, WSGI, Flask之间的关系
概览 之前对 Nginx,WSGI(或者 uWSGI,uwsgi),Flask(或者 Django),这几者的关系一存存在疑惑.通过查阅了些资料,总算把它们的关系理清了. 总括来说,客户端从发送一个 ...
- 如何让socket编程非阻塞?
import socket # 创建socket client = socket.socket() # 将原来阻塞的位置变成非阻塞(报错) client.setblocking(False) # 百度 ...
- JSP--JSP语法--指令---九大隐式对象--四大域对象--JSP内置标签--JavaBean的动作元素--MVC三层架构
一.JSP 原理:JSP其实就是一个servlet. Servlet负责业务逻辑处理,JSP只负责显示.开发中,JSP中不能有一行JAVA代码 二.JSP语法 1. JSP模板元素:JSP中HTML标 ...
- Angular学习笔记—路由(转载)
创建路由 1.首先安装 Angular Router.你可以通过运行以下任一操作来执行此操作: yarn add @angular/router # OR npm i --save @angular/ ...
- 简明python教程
linux查询python版本:python -V linux进入python:python 退出python:CTRL+D 使用源文件:helloworld.py 运行这个程序:python hel ...
- FileZilla Server IP限制设置
上面那个是黑名单. * 代表所有 下面那个是白名单. 多个是用 空格 分割
- PAT 天梯赛 L1-004. 计算摄氏温度 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-004 AC代码 #include <iostream> #include <cstdio&g ...
- js刷新页面 location.reload()
在javascript编程中,多使用location.reload实现页面刷新. 例子: 代码示例: window.location.href=window.location.href; window ...
- AFNetworking 3.0 解决加密后请求参数是字符串问题
把整个请求参数的json加密生成一个字符串传给服务器,错误提示:[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top ...
- Json日期格式 转化为 YYYY-MM-DD-hh-mm-ss
function timeStamp2String(time) { var datetime = new Date(); datetime.setTime(time); var year = date ...