Spring初学之annotation实现AOP前置通知和后置通知
实现两个整数的加减乘除,并在每个计算前后打印出日志。
ArithmeticCalculator.java:
package spring.aop.impl; public interface ArithmeticCalculator { int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j); }
ArithmeticCalculatorImpl.java:
package spring.aop.impl; import org.springframework.stereotype.Component; @Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator { public int add(int i, int j) {
int result = i+j;
return result;
} public int sub(int i, int j) {
int result = i-j;
return result;
} public int mul(int i, int j) {
int result = i*j;
return result;
} public int div(int i, int j) {
int result = i/j;
return result;
} }
LoggingAspect.java:
package spring.aop.impl; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面:1.把该类放入到IOC容器中,2.再声明一个切面 @Aspect
@Component
public class LoggingAspect { //声明该方法是一个前置通知:在目标方法之前执行
@Before("execution( public int spring.aop.impl.*.*(int,int) )")
public void beforeMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" begins"+args);
} //后置通知:在目标方法执行后执行(无论是否发生异常),的通知。
//在后置通知中不能访问目标方法的执行结果
@After("execution( public int spring.aop.impl.*.*(int,int) )")
public void afterMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends");
} }
ApplicationContext.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: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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="spring.aop.impl" /> <!-- 使AspectJ注解起作用,自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
测试:
package spring.aop.impl.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.aop.impl.ArithmeticCalculator; public class Main {
public static void main(String[] args) { //1.创建spring的IOC容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从容器中获取bean的实例
ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
//3.使用bean
int result = arithmeticCalculator.add(5, 8);
System.out.println("result:"+result); result = arithmeticCalculator.div(5, 8);
System.out.println("result:"+result);
}
}
输出:
The method add begins[5, 8]
The method add ends
result:13
The method div begins[5, 8]
The method div ends
result:0
Spring初学之annotation实现AOP前置通知和后置通知的更多相关文章
- Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。
实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...
- Spring AOP前置通知和后置通知
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...
- spring 基于xml的申明式AspectH中的后置通知的返回值获取
spring 基于xml的申明式AspectH中的后置通知的返回值获取 1. 配置文件 <aop:config> <aop:aspect ref="myAspect&quo ...
- Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等
实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- AOP 和 前置通知,后置通知
Spring 1.AOP:中文名称面向切面编程 2.英文名称:(Aspect Oriented Programming) 3.正常程序执行流程都是纵向执行流程 3.1 又叫面向切面编程,在原有纵向执行 ...
- Spring aop——前置增强和后置增强 使用注解Aspect和非侵入式配置
AspectJ是一个面向切面的框架,它扩展了java语言,定义了AOP语法,能够在编译期提供代码的织入,所以它有一个专门的编译器用来生成遵守字节码字节编码规范的Class文件 确保使用jdk为5.0以 ...
- spring学习 十 schema-based 前置后后置通知
spring 提供了 2 种 AOP 实现方式:(1)Schema-based ,(2)AspectJ Schema-based:每个通知都需要实现接口或类,配置 spring 配置文件时在<a ...
- spring框架应用系列四:切面编程(环绕通知与前后置通知区别)
切面编程(环绕通知与前后置通知区别) 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html 解决问 ...
随机推荐
- VLC 媒体播放器
VLC 媒体播放器 VLC 媒体播放器是一个便携式. 免费.开源. 跨平台的媒体播放器. VideoLAN 项目的流式媒体服务器.分为Windows Phone版本和Android版本. 下载地址: ...
- NodeJS版本EasyDarwin开源流媒体服务器开发心得
title: Node版本EasyDarwin开发心得 date: 2018-03-27 22:46:15 tags: 年后着手Node版本EasyDarwin的开发工作,截止到今天2018年03月2 ...
- IOS开发复习笔记(3)-ARC
1.ARC 当你自己调用了release或retain语句的时候,ARC有效时编译文件会遇到错误,你可以通过-fno-objc-arc和-fobjc-arc两个编译器标志在混搭中支持ARC和非ARC的 ...
- vs编译程序不能实现,“未能完成操作 未指定的错误”的解决办法
1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2005右边的“解决方案”处右键 重新加载,就 ...
- access 如何导出 cvs 文件?
三部曲 1 access 数据表导出 excel 表格 2 excel 另存为 *.cvs 格式文件 3 数据库导入 *.cvs 文件
- JS+PHP瀑布流效果(二)
<!-- 加载商品 --><script> //用户拖动滚动条,达到底部时ajax加载一次数据 var loading = $("#loading&quo ...
- mac安装yarn , MAC升级Nodejs
Npm i -g yarn 第一步,先查看本机node.js版本: `$ node -v` 第二步,清除node.js的cache: `$ sudo npm cache clean -f` 第三步,安 ...
- CentOS7更改网卡名称为eth0
#!/bin/bash # Author: fansik # Date: 2017年 09月 19日 星期二 :: CST sed -i 's@rhgb@rhgb net.ifnames=0@g' / ...
- 怎样在不对控件类型进行硬编码的情况下在 C#vs 中动态添加控件
文章ID: 815780 最近更新: 2004-1-12 这篇文章中的信息适用于: Microsoft Visual C# .NET 2003 标准版 Microsoft Visual C# .NET ...
- keepalived nginx 主备配置
keepalived nginx 主备配置(多主多备同理) 1.Nginx服务安装 nginx 不区分主备,在两台服务上安装两个即可. 安装参考:https://www.cnblogs.com/zw ...