什么是AOP:

Aop技术是Spring核心特性之中的一个,定义一个切面。切面上包括一些附加的业务逻辑代码。在程序运行的过程中找到一个切点,把切面放置在此处,程序运行到此处时候会运行切面上的代码。这就是AOP.。

AOP的实现机制是什么:

Aop是基于动态代理模式实现的,被代理类实现过接口,能够用反射包里的InvocationHandler和Proxy实现。被代理类没有实现过接口。能够用cglib生成二进制码获得代理类。

AOP有什么用途:

Aop用处许多,比方在程序中加入事物,加入日志等。

AOP怎样使用:

能够通过注解的方式和xml配置的方式使用Aop,例如以下是配置的Aop.

使用AOP。在UserDao的save方法前后,切入程序:

UserDao

package com.dao;

public class UserDao {
public void save(){
System.out.println("in save");
}
}

切面程序,在save方法运行前后要运行的程序:

package com.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class ServiceAop {
public void before(){
System.out.println("in before");
}
public void after(){
System.out.println("in after");
}
public void around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("around in before");
pjp.proceed();
System.out.println("around in after");
}
}

applicationContext.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:tx="http://www.springframework.org/schema/tx"
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"> <bean id="userDao" class="com.dao.UserDao"></bean>
<bean id="serviceAop" class="com.aop.ServiceAop"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.dao.*.*(..))" id="cutId"/>
<aop:aspect ref="serviceAop">
<aop:before method="before" pointcut-ref="cutId"/>
<aop:after method="after" pointcut-ref="cutId"/>
<aop:around method="around" pointcut-ref="cutId"/>
</aop:aspect>
</aop:config>
</beans>

測试类:

package com.dao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao ud=(UserDao) ac.getBean("userDao");
ud.save();
} }

Spring Aop基础总结的更多相关文章

  1. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  2. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  3. CgLib动态代理学习【Spring AOP基础之一】

    如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...

  4. 【AOP】Spring AOP基础 + 实践 完整记录

    Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...

  5. Spring AOP基础知识

    Spring AOP使用动态代理技术在运行期织入增强的代码,两种代理机制包括:一是基于JDK的动态代理,另一种是基于CGLib的动态代理.之所以需要两种代理机制,很大程度上是因为JDK本身只提供接口的 ...

  6. Java动态代理学习【Spring AOP基础之一】

    Spring AOP使用的其中一个底层技术就是Java的动态代理技术.Java的动态代理技术主要围绕两个类进行的 java.lang.reflect.InvocationHandler java.la ...

  7. Spring AOP基础概念及自定义注解式AOP初体验

    对AOP的理解开始是抽象的,看到切点的匹配方式其实与正则表达式性质大致一样就基本了解AOP是基本是个什么作用了.只是整个概念更抽象,需要具化理解.下图列表是AOP相关概念解释,可能也比较抽象^_^ 比 ...

  8. Spring AOP基础

    目录 AOP基本术语 Advice-通知 Before After After-returning After-throwing Around Pointcut-切点 Aspect-切面 Join P ...

  9. Spring AOP小记

    一.概述 在通常的开发过程中,我们调用的顺序通常是controller->service-dao,其中,service中包含着太多的业务逻辑,并且还要不断调用dao来实现自身的业务逻辑,经常会导 ...

随机推荐

  1. countdownlatch用法

    public void await() throws InterruptedException { };   //调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行 publ ...

  2. rsync运行时出现skipping non-regular file

    如果执行 rsync 时提示 skipping non-regular file……,检查下原文件夹中是否包含软链接 修改下脚本文件: rsync -va ... -a == -rlptgoD (no ...

  3. Java笔记11:JSP连接Oracle数据库

    1 建立Web项目 在D:\tomcat\webapps\中建立basicSyntax项目,在该项目中添加WEB-INF,WEB-INF\classes\,WEB-INF\lib\和WEB-INF\w ...

  4. 使用RMAN和控制文件备份删除归档日志的SHELL脚本--RED HAT 5 LINUX 64

    在ORACLE用户下的定时器设置 [oracle@SHARKDB dbscripts]$ crontab -l# minute hour day month week15 1  * * 0  sh / ...

  5. Sqlite-Sqlite3中的数据类型

    大多数的数据库引擎(到现在据我们所知的除了sqlite的每个sql数据库引擎)都使用静态的.刚性的类型,使用静态类型,数据的类型就由它的容器决定,这个容器是这个指被存放的特定列. Sqlite使用一个 ...

  6. Node.js aitaotu图片批量下载Node.js爬虫1.00版

    即使是https网页,解析的方式也不是一致的,需要多试试. 代码: //====================================================== // aitaot ...

  7. python3带参数的装饰器 函数参数类型检查

    from inspect import signature#python3才有的模块 def typeassert(*args,**kwargs): def decorator(fun): sig=s ...

  8. Tomcat从零开始(十七)——StandardWrapper

    第十七课:StandardWrapper 课前复习: 不知道大家是否还有印象,就是在6.7节课说的4种container,粗略的从大到小来说就是engine,host,context,和wrapper ...

  9. phpMyAdmim和Yii 连接Mysql报错。

    故障: 之前phpMyAdmim和Yii连接Mysql都好着的.某天,同一时候出现例如以下报错: 1.linux下phpMyAdmin 出现 "缺少 mysqli 扩展,请检查 PHP 配置 ...

  10. MongoDB 操作手冊CRUD 删除 remove

    删除记录 概述 在MongoDB中,db.collection.remove()方法用于删除集合中的记录.能够删除全部记录,删除全部符合条件的记录.或者是仅删除一条记录. 删除全部记录 删除一个集合中 ...