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来实现自身的业务逻辑,经常会导 ...
随机推荐
- C++多重继承二义性解决
1. 什么是多重继承的二义性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class A{ public: void f(); } class B{ pu ...
- 2011年排名前七位的Linux操作系统。
下面列出了2011年排名前七位的Linux操作系统. Ubuntu Ubuntu 是一个由全球化的专业开发团队建造的操作系统.它包含了所有您需要的应用程序:浏览器.Office 套件.多媒体程序.即时 ...
- Mac DBeaver Client home is not specified for connection解决办法
问题: 一般出现这个问题是由于DBeaver 程序无法自动找到数据库的安装目录,在进行数据的导入导出时无法执行,这是因为执行指令需要数据库相关命令的支持. 解决办法: 我用的是mysql,这里拿mys ...
- 使用SQL Server 扩展事件来创建死锁的跟踪
我们通过SQL Server 2014图形界面来部署一个扩展事件跟踪会话.然后可以生成SQL脚本. 步骤如下: 步骤1: 通过“对象资源管理器”连接到实例,展开“管理”.“扩展事件”.“会话”. 步骤 ...
- localstorge的缓存写法(超过一定时间自动清空)
使用缓存: (设置缓存,尽量用大写,下划线的写法) const ls = { set: function (variable, value, ttl_ms) { var data = {value: ...
- java 利用JAX-RS快速开发RESTful 服务实例
首先看web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...
- 能显示git分支的终端提示配置
之前都是跟随潮流,安装zsh然后oh-my-zsh,选一个看起来顺眼的主题,一通瞎配置,很酷炫. 可是即使只有一个git插件,oh-my-zsh每次启动的时候都很慢,起码有好几秒,而且有时候zsh还会 ...
- DataGridView绑定泛型List时,利用BindingList来实现增删查改
DataGridView绑定泛型List时,利用BindingList来实现增删查改 一. DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...
- C++ new的nothrow关键字和new_handler用法
C++ new的nothrow关键字和new_handler用法 new && new(std::nothrow) new(std::nothrow) 顾名思义,即不抛出异常,当new ...
- 关于Object.defineProperty的get和set
面试经常提问vue双向数据绑定的原理,其主要是依赖于Object.definePropety(); Object.definePropety下面有get和set方法. get指读取属性时调用的放法,s ...