Spring入门第十八课
Spring AOP
AspectJ:Java社区里最完整最流行的AOP框架
在Spring2.0以上的版本中,可以使用基于AspectJ注解或者基于XML配置的AOP
看代码:

package logan.study.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);
}
package logan.study.aop.impl; import org.springframework.stereotype.Component; @Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override
public int add(int i, int j) {
// TODO Auto-generated method stub
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
// TODO Auto-generated method stub
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
// TODO Auto-generated method stub
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
// TODO Auto-generated method stub
int result = i / j;
return result;
} }
package logan.study.aop.impl; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面
@Aspect
@Component
public class LoggingAspect {
//声明该方法时一个前置通知:在目标方法开始之前执行
@Before("execution(public int logan.study.aop.impl.ArithmeticCalculator.add(int, int))")
public void beforeMethod(){
System.out.println("The method begins");
} }
<?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="logan.study.aop.impl"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
package logan.study.aop.impl; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
//1.创建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器里面获取bean实例
ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
//使用Bean
int result = arithmeticCalculator.add(3, 6);
System.out.println(result); } }
输出结果:
五月 27, 2017 4:41:38 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sat May 27 16:41:38 CST 2017]; root of context hierarchy
五月 27, 2017 4:41:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
The method begins
9
在看代码:
package logan.study.aop.impl; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面
@Aspect
@Component
public class LoggingAspect {
//声明该方法时一个前置通知:在目标方法开始之前执行
@Before("execution(public int logan.study.aop.impl.ArithmeticCalculator.add(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 with "+args);
} }
输出结果:
五月 27, 2017 5:10:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sat May 27 17:10:24 CST 2017]; root of context hierarchy
五月 27, 2017 5:10:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
The method add begins with [3, 6]
9
在看代码:
package logan.study.aop.impl; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面
@Aspect
@Component
public class LoggingAspect {
//声明该方法时一个前置通知:在目标方法开始之前执行
@Before("execution(public int logan.study.aop.impl.ArithmeticCalculator.*(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 with "+args);
} }
package logan.study.aop.impl; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
//1.创建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器里面获取bean实例
ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
//使用Bean
int result = arithmeticCalculator.add(3, 6);
System.out.println(result); result = arithmeticCalculator.div(3, 6);
System.out.println(result); } }
输出结果:
五月 27, 2017 5:12:36 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sat May 27 17:12:36 CST 2017]; root of context hierarchy
五月 27, 2017 5:12:36 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
The method add begins with [3, 6]
9
The method div begins with [3, 6]
0
1.SpringAOP
加入jar包:
spring-aop-4.3.8.RELEASE.jar
spring-aspects-4.3.8.RELEASE.jar
spring-beans-4.3.8.RELEASE.jar
spring-context-4.3.8.RELEASE.jar
spring-core-4.3.8.RELEASE.jar
spring-expression-4.3.8.RELEASE.jar
commons-logging-1.2.jar
aspectjrt.jar
aspectjweaver.jar
aopalliance-1.0.jar
aspectj-1.8.10.jar
在配置文件中加入aop的命名空间
基于注解的方式:
在配置文件中加入如下配置:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
把横切关注点的代码抽象到切面的类中
切面首先是一个IOC中的Bean,即加入@Component注解
切面还需要加入@Aspect注解
在类中声明各种通知。
AspectJ支持5中类型的通知注解:
-@Before:前置通知,在方法执行之前执行
-@After:后置通知,在方法执行之后通知
-@AfterRunning:返回通知,在方法返回结果之后执行
-@AfterThrowing:异常通知:在方法抛出异常之后
-@Around:环绕通知,围绕着方法执行。
声明一个方法
在方法前加入@Before注解
利用方法签名编写AspectJ切入点表达式
最典型的切入点表达式时根据方法的签名来匹配各种方法:
-execution * logan.aop.ArithmeticCalculator.*(...)匹配ArithmeticCalculator中声明的所有方法,第一个*代表任意修饰符以及任意返回值,第二个*代表任意方法,(...)匹配任意数量的参数,若目标类与接口与切面在同一个包中,可以省略包名。
-execution public * ArithmeticCalculator.*(...):匹配ArithmeticCalculator接口的所有公有方法。
-execution public double ArithmeticCalculator.*(...):匹配ArithmeticCalculator中返回double类型数字的方法。
-execution public double ArithmeticCalculator.*(double,...):匹配ArithmeticCalculator中第一个参数为double类型的,返回double类型数字的方法。
-execution public double ArithmeticCalculator.*(double, double):匹配ArithmeticCalculator中参数类型为(double,double)类型的方法。
Spring入门第十八课的更多相关文章
- Spring入门第十九课
后置通知 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j ...
- Spring入门第十六课
接上一次讲课 先看代码: package logan.spring.study.annotation.repository; public interface UserRepository { voi ...
- Spring入门第十五课
泛型依赖注入 看代码: package logan.spring.study.generic.di; public class BaseRepository<T> { } package ...
- Spring入门第十四课
基于注解的方式配置bean(基于注解配置Bean,基于注解来装配Bean的属性) 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath ...
- Spring入门第十二课
Bean的配置方法 通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 通过调用静态工厂方法创建Bean 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户 ...
- Spring入门第十课
Spring表达式语言:SpEL Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都 ...
- Spring入门第二十八课
事务的传播行为 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播,例如:方法可能继续在现有事务中运行,也可能开启一个新的事务,并在自己的事务中运行. 事务的传播行为可以由传播属性指定.Spr ...
- NeHe OpenGL教程 第三十八课:资源文件
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第四十八课:轨迹球
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
随机推荐
- C# WinForm退出方法
1.this.Close(); 只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit(); 强制所有消息中 ...
- 【BZOJ4197】[Noi2015]寿司晚宴 状压DP+分解质因数
[BZOJ4197][Noi2015]寿司晚宴 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴 ...
- There are two different types of export, named and default
export - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statemen ...
- 2017-2018-1 20179209《Linux内核原理与分析》第二周作业
本周课业主要通过分析汇编代码执行情况掌握栈的变化.本人本科时期学过intel 80X86汇编语言,所以有一定基础:在Linux中32位AT&T风格的汇编稍微熟悉就可以明白.所以我学习的重点放在 ...
- linux撤销命令
u撤销上一步操作 ctrl+r恢复上一步被撤销的操作
- 游戏引擎基于Handle的资源管理
基于Handle的资源管理方案,第一时间想到的应该是Windows了,但是真正想让我实施这个方案的,是<游戏编程精粹1>里面的游戏资源管理篇章的给出的方案.在<游戏编程精粹1> ...
- 深入理解JVM - 垃圾收集器与内存分配策略 - 第三章
引用计数算法——判断对象是否存活的算法 很多教科书判断对象是否存活的算法是这样的:给对象添加一个引用计数器,每当一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象 ...
- tkinter之对话框
对话框的一个例子: from tkinter.dialog import * from tkinter import * def investigation(): d=Dialog(None,titl ...
- HDU 1005 Number Sequence:矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意: 数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n ...
- HttpClient_02_实现https协议
二.参考文档 1.Java 和 HTTP 的那些事(四) HTTPS 和 证书 1. 轻松把玩HttpClient之配置ssl,采用设置信任自签名证书实现https 2.如何使用HttpClient来 ...