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 解决问 ...
随机推荐
- 【BZOJ2325】[ZJOI2011]道馆之战 线段树+树链剖分
[BZOJ2325][ZJOI2011]道馆之战 Description 口袋妖怪(又名神奇宝贝或宠物小精灵)红/蓝/绿宝石中的水系道馆需要经过三个冰地才能到达馆主的面前,冰地中的每一个冰块都只能经过 ...
- #1589 : 回文子串的数量(Manacher)
#1589 : 回文子串的数量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个字符串S,请统计S的所有|S| * (|S| + 1) / 2个子串中(首尾位置不 ...
- ubuntu14.0 hadoop2.4.0 64位基于jdk1.7搭建
注意:hadoop有两种运行模式,安全模式和非安全模式.安装模式是以指定在健壮的,基于身份验证上运行的,本文无需运行在非安全模式下,可以直接使用root用户. 本文用户是基于root用户来运行的 一. ...
- MariaDB数据库主从复制实现步骤
一.MariaDB简介 MariaDB数据库的主从复制方案,是其自带的功能,并且主从复制并不是复制磁盘上的数据库文件,而是通过binlog日志复制到需要同步的从服务器上. MariaDB数据库支持单向 ...
- Python操作Redis(二)
List操作 redis中的List在在内存中按照一个name对应一个List来存储.如图: lpush(name,values) # 在name对应的list中添加元素,每个新的元素都添加到列表的最 ...
- windows中使用Findwindow函数与FindWindowEx函数来实现自动控制、触发第三方软件事件的方法
FindWindow 用来根据类名和窗口名来得到窗口句柄的.但是这个函数不能查找子窗口,也不区分大小写. 如果要从一个窗口的子窗口中查找需要使用FindWindowEX. 如果要搜索的外部程序的窗口标 ...
- docker学习笔记1-- 用Toolbox安装Docker--介绍Docker Machine
使用的是Docker Toolbox,非Docker for Windows 一.docker的认识与安装(windows安装) http://blog.csdn.net/tina_ttl/artic ...
- 预防SQL注入攻击
/** * 预防SQL注入攻击 * @param string $value * @return string */ function check_input($value) { // 去除斜杠 if ...
- iOS JS 和 OC交互 / JS 和 native 相互调用
现在app 上越来越多需求是通过UIWebView 来展示html 或者 html5的内容, js 和 native OC代码交互 就非常常见了. js 调用 native OC代码 第一种机制 ( ...
- ANSI编码——代码页
详见wiki: http://zh.wikipedia.org/wiki/%E4%BB%A3%E7%A0%81%E9%A1%B5