Spring的aop介绍
来源:互联网 发布:淘宝运营方案ppt模板 编辑:程序博客网 时间:2024/05/16 08:37
AOP(Aspect Oriented Programming):主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或者阶段,以获取逻辑过程中各部分之间的低耦合的隔离效果。常用于对日志记录。
———-如:老师添加一个学生信息到数据库中,我们为了数据的安全性需要将谁添加的那个同学的信息记录到日志文件中,方便我们程序人员查看信息是否安全。
这样就产生一个功能:添加日志信息
实现方法1:
public class addLog { public void addStudentLog(TeacherInformation teaInfo,StudentInformation stuInfo) { }}
然后在调用添加addStudent()之后再调用addStudentLog()方法,这样能够实现添加日志的功能
———-但是这里产生了一个问题,一个功能最好只实现本功能,也就是说不关心其他事件:本来我这个功能只需要添加学生信息就可以了,但是你将添加学生信息这个日志也放在这个功能里面就会产生错乱,尽管看起来不会有太的问题。但举个例子你就会知道在哪里有问题:明朝设立东厂,是世界上最早设立的国家特务情报机关,而这里的日志就相当于情报。如果一个机构既有审案功能又有编写情报的功能就会有很多的贪污出来。
所以要抽出一个切面出来,一旦我执行了添加学生的功能就能被我监视到,我就要添加一个日志
这里介绍spring里面的aop技术
1、首先介绍xml的配置
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.xingyao"/> <aop:aspectj-autoproxy/></beans>
然后你要定义一个切面,里面封装了需要执行的切面方法,也就是类似日志的方法功能
package com.xingyao.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
/** * ..代表无限多个层次,说明这个文件下的所有文件或者任何参数(不论个数与类型) * *代表任何文件或者任何返回值、任何方法 */@Before("execution(public * com.xingyao.dao..*.*(..))")public void before() { System.out.println("程序运行前");}
这里用的是before,说明在执行com.xingyao.dao下所有包下所有类文件下的返回值为任意类型的的所有方法前需要执行这个before()方法
这就将日志的添加抽象出来了,不需要每次都在方法里面调用,提高了代码的耦合性
- spring的AOP介绍
- Spring的AOP介绍
- Spring的aop介绍
- Spring的AOP介绍
- Spring AOP的简单介绍
- Spring中IOC和AOP的介绍
- Spring中AOP的Introductions使用介绍
- Spring AOP介绍
- Spring中AOP介绍
- spring aop介绍
- Spring AOP介绍
- Spring AOP介绍
- Spring AOP简要介绍
- 【spring框架】AOP介绍
- spring aop 简单介绍
- Spring AOP介绍
- Spring AOP介绍
- Spring AOP介绍
- 方舟子说中国很蒙昧
- 二级listview使用注意
- Eclipse web项目断点调试
- iOS小明开发笔记(三十) (应用性能调优的建议和技巧)
- Android根据URL下载文件保存到SD卡
- Spring的aop介绍
- android 相对布局RelativeLayout属性大全
- 判读一个年份是平年还是闰年
- tomcat端口占用无法启动的解决方法
- 进程间通讯的开发者指南
- openvpn
- bcb ListView的用法
- 正确使用#include和前置声明(forward declaration)
- C快速排序