一、引言:

  AspectJ框架不仅实现了面向切面编程,而且还支持注解,spring将它引入自己的规范之中。

二、需要了解:

  1. AspectJ是基于java语言的AOP框架
  2. spring2.0之后支持AspectJ切入点表达式
  3. 描述切点使用切入点表达式

三、通知的类型(重点)

四、利用AspectJ实现AOP计算代码执行时间戳

beans.xml文件

<!-- 目标类 -->
<bean id="userDaoImpl" class="com.xx.dao.UserDaoImpl"></bean>
<!-- 切面类 -->
<bean id="myAspect" class="com.xx.dao.MyAspect"></bean>
<!-- aop配置 -->
<aop:config>
<!-- 配置切面
<aop:aspect>:定义切面
<aop:pointcut>:目标类中的切点,即连接点中的切点
<aop:before>:通知类型共6种,分别为 前置、后置、环绕、异常、最终、引介
method:切面中的方法
pointcut-ref:引入切点
pointcut :自定义切点,局限于当前
-->
<aop:aspect ref="myAspect">
<aop:pointcut expression="execution(* com.xx.dao.UserDaoImpl.add*(..))" id="MyPointcut"/>
<aop:before method="before" pointcut-ref="MyPointcut"/>
<aop:after method="after" pointcut-ref="MyPointcut"/>
</aop:aspect>
</aop:config>

切面类:

package com.xx.dao;

public class MyAspect{

	private long beforeTime = 0;
private long afterTime = 0;
public void before(){
beforeTime = System.currentTimeMillis();
}
public void after(){
afterTime = System.currentTimeMillis();
System.out.println("运行时间"+(afterTime-beforeTime));
}
}

UserDao接口:

package com.xx.dao;

public interface UserDao {

	public void addUser();
public void delUser();
public void updateUser();
}

UserDaoImpl实现类:

package com.xx.dao;

public class UserDaoImpl implements UserDao{

	@Override
public void addUser() {
System.out.println("增");
}
@Override
public void delUser() {
System.out.println("删");
}
@Override
public void updateUser() {
System.out.println("改");
}
}

测试类:

package com.xx.dao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) { String xmlPath = "com/xx/dao/beans.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
UserDao userDao = (UserDao) context.getBean("userDaoImpl");
userDao.addUser();
userDao.delUser();
userDao.updateUser();
}
}

spring之AspectJ基于xml AOP编程的更多相关文章

  1. spring之AspectJ基于注解 AOP编程

    一.前言 使用注解代替之前在spring配置文件中配置目标类.切面类和aop配置. 二.注意 需要注意的是,需要在spring配置文件中引入如下,如果不添加,切面类中的@Aspect注解将不起作用 & ...

  2. spring的AspectJ基于XML和注解(前置、后置、环绕、抛出异常、最终通知)

    1.概念 (1)AspectJ是一个基于Java语言的AOP框架 (2)Spring2.0以后新增了对AspectJ切入点表达式的支持 (3)AspectJ是AspectJ1.5的新增功能,通过JDK ...

  3. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  4. SSM-Spring-18:Spring中aspectJ的XML版

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- aspectJ的xml版是开发中最常用的: 下面直接已案例入手,毕竟繁琐的日子不多了 案例:两个接口,俩个实现 ...

  5. AspectJ基于xml和基于注解

    一.基于xml 执行的切入点中具体方法有返回值,则方法结束会立即执行后置通知,然后再执行环绕通知的放行之后的代码: 2.连接点即所有可能的方法,切入点是正真被切的方法,连接点方法名: 其中,只有环绕通 ...

  6. 【原】Spring和Dubbo基于XML配置整合过程

    背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时,只需一个 ...

  7. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  8. Spring 框架基础(04):AOP切面编程概念,几种实现方式演示

    本文源码:GitHub·点这里 || GitEE·点这里 一.AOP基础简介 1.切面编程简介 AOP全称:Aspect Oriented Programming,面向切面编程.通过预编译方式和运行期 ...

  9. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...

随机推荐

  1. Sql的基础知识(一)

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份 sql server --- ...

  2. CSS3对于盒中容纳不下的内容的显示——overflow属性

    overflow属性 1.如果将overflow属性值设定为hidden,则超出容纳范围的文字将被隐藏起来. div{ overflow:hidden; } 2.如果将overflow属性值设定为sc ...

  3. CSS3中only-child伪类选择器

    <body> <style type="text/css"> //只对li1设置样式 li:nth-child(1):nth-last-child(1){ ...

  4. Android应用程序使用两个LinearLayout编排5个Button控件

    学习存档: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...

  5. 2017 Multi-University Training Contest - Team 1 1001&&HDU 6033 Add More Zero【签到题,数学,水】

    Add More Zero Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  6. Vijos P1114 FBI树【DFS模拟,二叉树入门】

    描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...

  7. A. Vasya and Football

    A. Vasya and Football time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. python内建函数isinstance基础用法

      语法:isinstance(object,type) 作用:来判断一个对象是否是一个已知的类型.  其第一个参数(object)为对象,第二个参数(type)为类型名(int...)或类型名的一个 ...

  9. 通过案例理解position:relative和position:absolute

    w3school过了HTML的知识之后,觉得要自己单纯地去啃知识点有点枯燥,然后自己也很容易忘记,所以便找具体的网站练手便补上不懂的知识点.position:relative和postion:abso ...

  10. TI-RTOS 之 事件同步(Event, 类似semaphore)

    TI-RTOS 之 事件同步(Event, 类似semaphore) Event 是类似Semaphore的存在,官方如下描述: SYS/BIOS events are a means of comm ...