一、AOP是OOP的延续,是(Aspect Oriented Programming)的缩写,意思是面向切面编程。

AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,

如事务管理、安全检查、缓存、对象池管理等。 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,

AOP 代理则可分为静态代理和动态代理两大类,其中静态代理是指使用 AOP 框架提供的命令进行编译,

从而在编译阶段就可生成 AOP 代理类,因此也称为编译时增强;

而动态代理则在运行时借助于 JDK 动态代理、CGLIB 等在内存中"临时"生成 AOP 动态代理类,因此也被称为运行时增强

AOP 的实现:

MyInterceptor、MyInterceptor2分别是以annotations和xml定义的切面类

package com.service;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

@Aspect

public class MyInterceptor {

@Pointcut("execution (* com.serviceImpl.PersonServiceImpl.*(..))")

private void myMethod(){};

@Before("myMethod()")

public void doAccessCheck(){

System.out.println("before");

}

}

[java] view plain copy

package com.service;

public class MyInterceptor2 {

public void doAccessCheck(){

System.out.println("before");

}

}

业务和接口

[java] view plain copy

package com.service;

public interface PersonService {

public void save(String name);

public void update(String name);

}

[java] view plain copy

package com.serviceImpl;

import com.service.PersonService;

public class PersonServiceImpl implements PersonService {

@Override

public void save(String name) {

// TODO Auto-generated method stub

System.out.println("保存");

}

@Override

public void update(String name) {

// TODO Auto-generated method stub

System.out.println("修改");

}

}

简单做个方法前通知,其他的都一样。

[java] view plain copy

<?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-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<aop:aspectj-autoproxy/>

<bean id="personServiceImpl" class="com.serviceImpl.PersonServiceImpl"></bean>

<bean id="personInterceptor" class="com.service.MyInterceptor2"></bean>

<aop:config>

<aop:aspect id="asp" ref="personInterceptor">

<aop:pointcut id="myCut" expression="execution (* com.serviceImpl.PersonServiceImpl.*(..))"/>

<aop:before pointcut-ref="myCut" method="doAccessCheck"/>

</aop:aspect>

</aop:config>

</beans>

测试类

[java] view plain copy

package com.test;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.service.PersonService;

public class AopTest {

@Test

public void interceptorTest(){

ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

PersonService ps = (PersonService) ac.getBean("personServiceImpl");

ps.save("aa");

}

}

最新文章

  1. jQuery学习-打字游戏
  2. SharePoint Server 2013开发之旅(三):为SharePoint Server配置App开发、部署、管理环境
  3. Codeforces VK Cup 2012 Round 3 A. Variable, or There and Back Again(dfs)
  4. 一些常见maven仓库
  5. 总结使用Unity 3D优化游戏运行性能的经验
  6. uva11082 Matrix Decompressing
  7. Asp.Net中的三种分页方式
  8. IOS web网页图片上传问题
  9. Effective C++ 条款11
  10. 统计分析SQL Server Profiler 跟踪的SQL
  11. vb编程代码大全
  12. codevs 2621 土地侵蚀
  13. js练习
  14. 通过arcmap发布缓存服务,无法选择自定义方案
  15. [Android] QPST,解BL锁,刷Recovery,备份系统,root,刷框架.
  16. [J2EE]struts+ejb笔记
  17. [javaSE] 集合框架(体系概述)
  18. TF 设置GPU模式训练
  19. 从浏览器输入URL到页面渲染的过程
  20. 1305. [CQOI2009]跳舞【最大流+二分】

热门文章

  1. 自制操作系统Antz(9)——实现内核 (下) 实现图形化界面
  2. 安全检查,Windows更新出现8024402F错误如何解决
  3. oracle 审计功能
  4. spring boot 2使用Mybatis多表关联查询
  5. lua相关的小知识
  6. Dva三种方式实现dispatch的Promise回调
  7. git commit -m 提交的内容换行
  8. 通过 ssh 建立本地与 github 的连接
  9. PyQt5——基本控件
  10. Jmeter转换成中文模式