我们看基于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;

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 java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint; public class LoggingAspect { 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);
} public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+ methodName +" ends with "+args);
} public void afterReturningMethod(JoinPoint joinPoint,Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+ methodName +" ends with "+result);
} public void afterThrowingMethod(JoinPoint joinPoint,Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+ methodName +" occus with "+ex);
} }
package logan.study.aop.impl;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Before;
public class VlidationAspect {
@Before("LoggingAspect.declareJoinPointException()")
public void validateArgs(JoinPoint joinPoint){
System.out.println("validate: " + Arrays.asList(joinPoint.getArgs()));
} }
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <bean id="arithmeticCalculator"
class="logan.study.aop.impl.ArithmeticCalculatorImpl"></bean> <bean id="loggingAspect"
class="logan.study.aop.impl.LoggingAspect"></bean> <bean id="vlidationAspect"
class="logan.study.aop.impl.VlidationAspect"></bean> <aop:config>
<aop:pointcut expression="execution(* logan.study.aop.impl.ArithmeticCalculatorImpl.*(int, int))"
id="pointcut"/>
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturningMethod" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
</aop:aspect>
<aop:aspect ref="vlidationAspect" order="1">
<aop:before method="validateArgs" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config> </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.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, 1);
System.out.println(result); } }

返回结果:

五月 28, 2017 7:53:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sun May 28 07:53:29 CST 2017]; root of context hierarchy
五月 28, 2017 7:53:29 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext-xml.xml]
validate: [3, 6]
The method add begins with [3, 6]
The method add ends with [3, 6]
The method add ends with 9
9
validate: [3, 1]
The method div begins with [3, 1]
The method div ends with [3, 1]
The method div ends with 3
3

稍微修改代码:

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.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, 0);
System.out.println(result); } }

返回结果:

五月 28, 2017 7:56:58 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sun May 28 07:56:58 CST 2017]; root of context hierarchy
五月 28, 2017 7:56:58 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext-xml.xml]
validate: [3, 6]
The method add begins with [3, 6]
The method add ends with [3, 6]
The method add ends with 9
9
validate: [3, 0]
The method div begins with [3, 0]
The method div ends with [3, 0]
The method div occus with java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at logan.study.aop.impl.ArithmeticCalculatorImpl.div(ArithmeticCalculatorImpl.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy2.div(Unknown Source)
at logan.study.aop.impl.Main.main(Main.java:18)

Spring入门第二十三课的更多相关文章

  1. Spring入门第二十七课

    声明式事务 直接上代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Dri ...

  2. Spring入门第二十一课

    切面优先级 先看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int ...

  3. Spring入门第二十课

    返回通知,异常通知,环绕通知 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(in ...

  4. NeHe OpenGL教程 第二十三课:球面映射

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  5. Spring入门第二课

    看代码 package logan.spring.study; public class HelloWorld { private String name; public void setName2( ...

  6. Spring入门第二课:Spring配置Bean的细节

    1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...

  7. Spring入门第二十九课

    事务的隔离级别,回滚,只读,过期 当同一个应用程序或者不同应用程序中的多个事务在同一个数据集上并发执行时,可能会出现许多意外的问题. 并发事务所导致的问题可以分为下面三种类型: -脏读 -不可重复读 ...

  8. Spring入门第二十八课

    事务的传播行为 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播,例如:方法可能继续在现有事务中运行,也可能开启一个新的事务,并在自己的事务中运行. 事务的传播行为可以由传播属性指定.Spr ...

  9. Spring入门第二十六课

    Spring中的事务管理 事务简介 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性. 事务就是一系列的动作,他们被当做一个单独的工作单元,这些动作要么全部完成,要么全部不起 ...

随机推荐

  1. jquery获取表单元素与回显

    一.获取哦表单元素 dcoument表单文本对象的集合 all[] 对所有html元素的访问 forms 返回对文档中所有form对象的引用 forms[1] 对所有form对象引用 <scri ...

  2. POJ 之2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20003   Accepted: 10063 D ...

  3. nginx Unit 服务器

    转自: https://github.com/nginx/unit/pull/18/ 感谢: https://www.v2ex.com/t/389528 English 简体中文 繁體中文 NGINX ...

  4. bootstraptable的 showFooter属性

    如果想在表格最下面显示统计的信息可以使用这个属性  首先 先在表格加上这个属性 showFooter:true, 然后 在需要的列里面新增属性 footerFormatter  设置列的名称 然后在需 ...

  5. Spark操作算子本质-RDD的容错

    Spark操作算子本质-RDD的容错spark模式1.standalone master 资源调度 worker2.yarn resourcemanager 资源调度 nodemanager在一个集群 ...

  6. Cocos2dx版本与CocosBuilder版本匹配问题

    我用的是CocosBuilder 2.1版本,将ccbi导入到xcode 5中时提示: WARNING! Incompatible ccbi file version 警告;ccbi版本不兼容. 查看 ...

  7. CommonJS、AMD与CMD

    自从有了模块,我们可以更方便地使用别人的代码,想要什么功能,就加载什么模块.但是,这样做有一个前提,那就是大家必须以同样的方式编写模块,否则你有你的写法,我有我的写法,岂不是乱了套! 于是下面三个模块 ...

  8. HTML5 canvas save()和restore()方法讲解

    我们尝试用这个连续矩形的例子来描述 canvas 的状态堆是如何工作的.第一步是用默认设置画一个大四方形,然后保存一下状态.改变填充颜色画第二个小一点的白色四方形,然后再保存一下状态.再次改变填充颜色 ...

  9. VS2013修改resource之后产生designer1.cs

    1. Unload project2. Edit the csproj file.3. Search for <LastGenOutput>test1.Designer.cs</La ...

  10. 关于MFC预处理命令

    MFC程序生成EXE文件的过程是:预处理-编译-链接-打包生成exe文件.(预编译是编译过程,即将一些常用的不经常改变的文件先进行编译处理生成中间文件,以节省时间,它不属于预处理,在VS项目属性的C/ ...