导包等不在赘述;

建立一个接口:ArithmeticCalculator,没有实例化的方法;

package com.atguigu.spring.aop.impl.panpan;

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 继承于接口:ArithmeticCalculator,实现接口中没有实例化的方法

package com.atguigu.spring.aop.impl.panpan;

import org.springframework.stereotype.Component;

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    @Override
public int add(int i, int j) {
int result=i+j;
return result;
} @Override
public int sub(int i, int j) {
int result=i-j;
return result;
} @Override
public int mul(int i, int j) {
int result=i*j;
return result;
} @Override
public int div(int i, int j) {
int result=i/j;
return result;
} }

建立类:ArithmeticCalculatorLoggingProxy,实例化一些面向切面编程的方法

package com.atguigu.spring.aop.impl.panpan;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class ArithmeticCalculatorLoggingProxy { //前置通知的方法,在xml文件中可以通过method这个属性获取该方法的方法名,joinPoint是切点,
public void beforeMethod(JoinPoint joinPoint){
//获取方法名,和参数值,参数值要多个,所以用数组集合的方法
String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs(); System.out.println("The method "+methodName+" begains "+Arrays.asList(args));
} //后置通知的方法
public void afterMethod(JoinPoint joinPoint){
//获取方法名,和参数值
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+ " ends");
} //返回通知的方法
public void afterReturning(JoinPoint joinPoint,Object result){
//获取方法名,和参数值
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+ " *** ends :"+result);
} //异常通知的方法
public void afterThrowing(JoinPoint joinPoint, Exception e){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+ " Exception :"+ e);
} //环绕通知的
public Object aroundMethod(ProceedingJoinPoint pjp){
Object result=null;
//获取方法名
String methodName=pjp.getSignature().getName();
try {
//前置通知,Arrays.asList(pjp.getArgs())为该方法的参数个数,为数组集合
System.out.println("The method "+methodName+" begains "+Arrays.asList(pjp.getArgs())); //执行目标方法
result=pjp.proceed(); //返回通知
System.out.println("The method "+methodName+ " ends with :"+result); } catch (Throwable e) {
//异常通知
System.out.println("The method " +methodName+ " occurs exception "+ e);
e.printStackTrace();
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
}

建立spring的xml文件,进行bean和AOP的配置

<!-- 配置bean ,全类名是继承接口类的全类名-->
<bean id="arithmeticCalculator"
class="com.atguigu.spring.aop.impl.panpan.ArithmeticCalculatorImpl"></bean> <!-- 配置切面的bean ,全类名是实现切面编程的类全类名-->
<bean id="arithmeticCalculatorLoggingProxy"
class="com.atguigu.spring.aop.impl.panpan.ArithmeticCalculatorLoggingProxy"></bean> <!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式,全类名为接口的全类名 -->
<aop:pointcut expression="execution(* com.atguigu.spring.aop.impl.panpan.ArithmeticCalculator.*(int,int))"
id="pointcut"/> <!-- 配置切面及通知 method中的为方法名,-->
<aop:aspect ref="arithmeticCalculatorLoggingProxy" order="1">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:around method="afterMethod" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>

建立类Main,进行测试

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.test.xml");
//ArithmeticCalculator,是一个接口类,注解的是继承该接口的类
ArithmeticCalculator impl=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator"); System.out.println(impl.getClass().getName()); int result=impl.add(12,2);
System.out.println(result); double result2=impl.div(12, 2);
System.out.println(result2);

Spring AOP:面向切面编程,AspectJ,是基于spring 的xml文件的方法的更多相关文章

  1. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...

  2. 【原创】Android AOP面向切面编程AspectJ

    一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...

  3. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  4. spring AOP面向切面编程学习笔记

    一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...

  5. 【Spring系列】Spring AOP面向切面编程

    前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...

  6. Spring AOP面向切面编程详解

    前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...

  7. Spring Aop面向切面编程&&自动注入

    1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...

  8. 从源码入手,一文带你读懂Spring AOP面向切面编程

    之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...

  9. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  10. Spring AOP 面向切面编程入门

    什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...

随机推荐

  1. 使用已有PDB克隆PDB

    使用已有PDB克隆PDB $ sqlplus '/as sysdba' SQL*Plus: Release Production on Tue Jun :: Copyright (c) , , Ora ...

  2. 改变对update的做法

    以前都是 先根据id或者其他条件查出来  再根据查出来的结果 进行修改  再update提交 这里可以改所有的字段 现在是做法 是直接new 一个 Do或者Vo  把要改变的值 先填充进去  然后再去 ...

  3. eclipse启动无响应,停留在Loading workbench状态

    做开发的同学们或多或少的都会遇到eclipse启动到一定程度时,就进入灰色无响应状态再也不动了.启动画面始终停留在Loading workbench状态.反复重启,状态依旧. 多数情况下,应该是非正常 ...

  4. Java基础之一组有用的类——使用比较器对数组排序(TrySortingWithComparator)

    控制台程序. Arrays类中的sort()静态方法把传送为参数的数组元素按升序方式排序. 对于第一个参数类型是Object[]的sort()方法来说,可以传送任意类型的数组.如果使用sort()方法 ...

  5. 3.HelloWorld

    准备开发环境和运行环境开发工具:eclipse运行环境:apache-tomcat-7.0.4工程:动态web 工程Spring 框架下载:spring-framework-3.2.3.RELEASE ...

  6. views of postgresql user password and encrypted or unencrypted

    password_encryption = onpostgres=# create user user1 with encrypted password 'user1';CREATE ROLEpost ...

  7. JSP out乱码

    在form method="post" 写post 在接受页面jsp代码前面加request.setCharacterEncoding("UTF-8");

  8. zjuoj 3600 Taxi Fare

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3600 Taxi Fare Time Limit: 2 Seconds    ...

  9. .NET: C#: 获取当前路径

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  10. javascript语法详解

    javascript语法:运算符 条件语句if...else...  条件语句switch  循环语句for  循环语句while   跳转语句 js运算符 1.算数运算符:+ - * % / ++ ...