Spring Aop基础总结
什么是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基础总结的更多相关文章
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- CgLib动态代理学习【Spring AOP基础之一】
如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...
- 【AOP】Spring AOP基础 + 实践 完整记录
Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...
- Spring AOP基础知识
Spring AOP使用动态代理技术在运行期织入增强的代码,两种代理机制包括:一是基于JDK的动态代理,另一种是基于CGLib的动态代理.之所以需要两种代理机制,很大程度上是因为JDK本身只提供接口的 ...
- Java动态代理学习【Spring AOP基础之一】
Spring AOP使用的其中一个底层技术就是Java的动态代理技术.Java的动态代理技术主要围绕两个类进行的 java.lang.reflect.InvocationHandler java.la ...
- Spring AOP基础概念及自定义注解式AOP初体验
对AOP的理解开始是抽象的,看到切点的匹配方式其实与正则表达式性质大致一样就基本了解AOP是基本是个什么作用了.只是整个概念更抽象,需要具化理解.下图列表是AOP相关概念解释,可能也比较抽象^_^ 比 ...
- Spring AOP基础
目录 AOP基本术语 Advice-通知 Before After After-returning After-throwing Around Pointcut-切点 Aspect-切面 Join P ...
- Spring AOP小记
一.概述 在通常的开发过程中,我们调用的顺序通常是controller->service-dao,其中,service中包含着太多的业务逻辑,并且还要不断调用dao来实现自身的业务逻辑,经常会导 ...
随机推荐
- toFixed()方法
1.定义 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 2.示例 Show the number 13.37 with one decimal: <script t ...
- React个人学习笔记
元素渲染 通过 ReactDOM.render() 方法渲染页面, 可以使用 ES6 class 来定义一个组件: 如何解析HTMl里面的空格: 1. 使用空格的 unicod 编码 : \u0020 ...
- [转]SSIS cannot convert between unicode and non-unicode string
本文转自:http://www.mssqltips.com/sqlservertip/1393/import-excel-unicode-data-with-sql-server-integratio ...
- IDEA java开发 Restful 风格的WebService
官网:https://www.jetbrains.com/help/idea/restful-webservices.html 1.在IntelliJ中创建新项目,选择Java Enterprise ...
- FreeMarker最简单的例子(1)
一.通过文件来创建模版对象,并执行插值操作 文件结构为: Test.Java中的代码为: package com.abc; import freemarker.template.Configurati ...
- CentOS6.8 4.4.43内核 安装PF_RING
环境: 系统:CentOS 6.8 内核版本:4.4.43 PF_RING版本:6.9.0 编译PF_RING需要内核源码,由于我的机器上只有4.4.43版本的modules和4.4.43的源码,并没 ...
- 切换样式.toggleClass()
切换样式.toggleClass() 在做某些效果的时候,可能会针对同一节点的某一个样式不断的切换,也就是addClass与removeClass的互斥切换,比如隔行换色效果 jQuery提供一个to ...
- Windows下Android开发环境配置
最新更新,见我新博客http://www.hrwhisper.me/java-android-environment-genymotion-emulator/ 更新了使用使用genymotion神级模 ...
- 流媒体播放mime类型添加
.m3u8 application/x-mpegURL.ts video/MP2T
- list/tuple/dict/set
一.list(列表) 内置类型,长度可变的有序集合,索引从0开始,索引为负数是标识从右开始取,最右边第一个是-1,以此类推.里面的元素可以是不同类型的. 1.定义:a = [] #空列表 2.获取长度 ...