一、引言:

  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. 51 nod 1227 平均最小公倍数

    原题链接 Lcm(a,b)表示a和b的最小公倍数,A(n)表示Lcm(n,i)的平均数(1 <= i <= n), 例如:A(4) = (Lcm(1,4) + Lcm(2,4) + Lcm ...

  2. [51nod1457]小K vs. 竹子

    小K的花园种着n颗竹子(竹子是一种茎部中空并且长得又高又快的热带植物).此时,花园中第i颗竹子的高度是hi米,并且在每天结束的时候它生长ai米. 实际上,小K十分讨厌这些竹子.他曾经试图去砍光它们,但 ...

  3. [hdu5225][BC#40]Tom and permutation

    好久没写题解了..GDKOI被数位DP教做人了一发,现在终于来填数位DP的大坑了>_<. 发现自己以前写的关于数位DP的东西...因为没结合图形+语文水平拙计现在已经完全看不懂了嗯. 看来 ...

  4. BZOJ3529: [Sdoi2014]数表

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3529 挺恶心的数论TAT... 设f[i]是i的约数和,这个可以nln(n)扫出来. ans= ...

  5. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  6. Spark环境搭建(上)——基础环境搭建

    Spark摘说 Spark的环境搭建涉及三个部分,一是linux系统基础环境搭建,二是Hadoop集群安装,三是Spark集群安装.在这里,主要介绍Spark在Centos系统上的准备工作--linu ...

  7. str_repeat() 函数把字符串重复指定的次数。

    str_repeat() 函数把字符串重复指定的次数. str_repeat(string,repeat) 参数 描述 string 必需.规定要重复的字符串. repeat 必需.规定字符串将被重复 ...

  8. PHP defined() 函数

    定义和用法 defined() 函数检查某常量是否存在. 若常量存在,则返回 true,否则返回 false. 语法 defined(name) 参数 描述 name 必需.规定要检查的常量的名称. ...

  9. 织梦CMS搭建网站必做的服务器相关安全设置

    http://help.dedecms.com/install-use/server/2011/1109/2124.html#printSource http://www.aliweihu.com/9 ...

  10. React Native学习(八)—— 对接七鱼客服

    本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...