Spring第十篇—举例实现AOP
简述AOP
AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。OOP引入封装、继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合。当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力。也就是说,OOP允许你定义从上到下的关系,但并不适合定义从左到右的关系。例如日志功能。日志代码往往水平地散布在所有对象层次中,而与它所散布到的对象的核心功能毫无关系。对于其他类型的代码,如安全性、异常处理和透明的持续性也是如此。这种散布在各处的无关的代码被称为横切(cross-cutting)代码,在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。而AOP技术则恰恰相反,它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其名为“Aspect”,即切面。所谓“切面”,简单地说,就是将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。
使用“横切”技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处都基本相似。比如权限认证、日志、事务处理。Aop 的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。正如Avanade公司的高级方案构架师Adam Magee所说,AOP的核心思想就是“将应用程序中的商业逻辑同对其提供支持的通用服务进行分离。”
AOP相关概念
切面(aspect):用来切插业务方法的类。
连接点(joinpoint):是切面类和业务类的连接点,其实就是封装了业务方法的一些基本属性,作为通知的参数来解析。
通知(advice):在切面类中,声明对业务方法做额外处理的方法。
切入点(pointcut):业务类中指定的方法,作为切面切入的点。其实就是指定某个方法作为切面切的地方。
目标对象(target object):被代理对象。
AOP代理(aop proxy):代理对象。
通知:
前置通知(before advice):在切入点之前执行。
后置通知(after returning advice):在切入点执行完成后,执行通知。
环绕通知(around advice):包围切入点,调用方法前后完成自定义行为。
异常通知(after throwing advice):在切入点抛出异常后,执行通知。
Weave(织入):指将aspects应用到target对象并导致proxy对象创建的过程称为织入.
PS:织入是关键,通过织入语法,产生proxy对象,如果类有接口就通过JDK创建代理对象,如果类没有接口则使用cglib创建代理对象。因为电脑JDK的版本,我使用的是Spring4.0。但因为我只学习了Spring3.2,所以我是拿着Spring4.0写着Spring3.2的代码。另外因为我们可能要经常修改服务的需求,例如测一下代码运行时间,所以使用XML的方式来实现AOP比注解的方式更好(毕竟如果我们没有源码,使用注解的方式不能在进行修改服务)。
示例:
配置文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" >
<context:component-scan base-package="net.zmcheng"/>
<bean id=" logInterceptor" class="net.zmcheng.aop.LogInterceptor"/>
<aop:config>
<aop:pointcut expression="execution(public * net.zmcheng.serviceImpl.UserServiceImpl.add())" id="servicePointCut"/>
<aop:aspect id="logAspect" ref=" logInterceptor">
<aop:before method="before" pointcut-ref="servicePointCut"/>
<aop:around method="aroundMethod" pointcut-ref="servicePointCut"/>
</aop:aspect>
</aop:config>
</beans>
|
需要使用的jar包:

切面类:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package net.zmcheng.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class LogInterceptor {
public void myMethod(){};
public void before(){
System.out.println("method start");
}
public void aroundMethod(ProceedingJoinPoint pjp){
System.out.println("around start");
try {
pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("around end");
}
}
|
服务层:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package net.zmcheng.service;
public interface UserService {
public void add();
}
package net.zmcheng.serviceImpl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import net.zmcheng.dao.UserDao;
import net.zmcheng.service.UserService;
@Component
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
userDao.add();
}
}
|
DAO层:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package net.zmcheng.dao;
public interface UserDao {
public void add();
}
package net.zmcheng.daoImpl;
import org.springframework.stereotype.Component;
import net.zmcheng.dao.UserDao;
@Component
public class UserDaoImpl implements UserDao {
public void add(){
System.out.println("添加员工成功");
}
}
|
测试类:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package net.zmcheng.demo.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import net.zmcheng.service.UserService;
import net.zmcheng.serviceImpl.UserServiceImpl;
public class SpringTest {
@Test
public void T() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService us = (UserService)ctx.getBean("userServiceImpl");
us.add();
}
}
|
执行结果:
method start
around start
添加员工成功
around end
Spring第十篇—举例实现AOP的更多相关文章
- 白话Spring(基础篇)---AOP(execution表达式)
作为AOP的最后一节内容,我们来简单总结一下切面表达式上见的书写方法.下面的那内容有参考其他博文,在此先对开源博客的各位大神表示感谢! -------------------------------- ...
- 白话Spring(基础篇)---AOP(execution表达式)(转)
[一知半解,就是给自己挖坑] 作为AOP的最后一节内容,我们来简单总结一下切面表达式上见的书写方法.下面的那内容有参考其他博文,在此先对开源博客的各位大神表示感谢! ----------------- ...
- Spring第六篇【Spring AOP模块】
前言 Spring的第五篇也算是AOP编程的开山篇了,主要讲解了代理模式-..本博文主要讲解Spring的AOP模块:注解方式和XML方式实现AOP编程.切入点表达式.. AOP的概述 Aop: as ...
- Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式
背景: 1)指定切面优先级示例:有的时候需要对一个方法指定多个切面,而这多个切面有时又需要按照不同顺序执行,因此,切面执行优先级别指定功能就变得很实用. 2)重复使用切入点表达式:上一篇文章中,定义前 ...
- Spring第六篇---AOP
接着Spring第五篇讲 我们今天将叙述以下几个知识点 1 什么是AOP AOP 是一种思想 横向重复 纵向抽取 在软件业,AOP为Aspect Oriented Programming的缩写,意 ...
- Spring Cloud第十篇 | 分布式配置中心Config
本文是Spring Cloud专栏的第十篇文章,了解前九篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇
Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...
- Java框架spring 学习笔记(十二):aop实例操作
使用aop需要在网上下载两个jar包: aopalliance.jar aspectjweaver.jar 为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将 ...
- Spring 学习十五 AOP
http://www.hongyanliren.com/2014m12/22797.html 1: 通知(advice): 就是你想要的功能,也就是安全.事物.日子等.先定义好,在想用的地方用一下.包 ...
随机推荐
- [百科] - SIP(会话发起协议)
SIP(会话发起协议)SIP是类似于HTTP的基于文本的协议.SIP可以减少应用特别是高级应用的开发时间.由于基于IP协议的SIP利用了IP网络,固定网运营商也会逐渐认识到SIP技术对于他们的深远意义 ...
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.2.build124)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build124 编译日期:2014-08-19 ====================================== 二. ...
- Educational Codeforces Round 15 C. Cellular Network(二分)
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 每天php函数 - str_replace()
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) 该函数返回一个字符 ...
- PHP 错误与异常 笔记与总结(8)自定义错误处理函数 set_error_handler()
通过 Set_error_handler() 函数设置用户自定义的错误处理函数. 步骤: ① 创建错误处理函数 ② 设置不同级别调用函数 ③ Set_error_handler() 函数制定接管错误处 ...
- Linux 计划任务 Crontab 笔记与总结(4)crontab 的日志
/var/log/cron 文件保存 cron 的任务执行记录 cd /var/log/ ls -l cron* 会发现每天都会有 cron 日志的变化 使用 tail -f cron 能够查看今天的 ...
- Linux 执行ThinkPHP 文件的计划任务
执行例如 http://192.168.1.32/index.php?s=/Home/Cron/yue 这样的 url 的计划任务 方式① */ * * * * curl http://192.168 ...
- PHP Error 和 Logging 函数
PHP Error 和 Logging 函数 PHP Error 和 Logging 简介 Error 和 Logging 函数允许您对错误进行处理和记录. Error 函数允许用户定义错误处理规则, ...
- Unity 中场景切换
Unity游戏开发中,单个Scene解决所有问题似乎不可能,那么多个Scene之间的切换是必然存在.如果仅仅是切换,似乎什么都好说,但是在场景比较大的时候不想让玩家等待加载或者说场景与场景之间想通过一 ...