【Spring AOP】AOP的实现(三)
一、Spring 对AOP的支持
Spring中AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。因此,AOP代理可以直接使用容器中的其它bean实例作为目标,这种关系可由IOC容器的依赖注入提供。Spring创建代理的规则为:
- 默认使用Java动态代理来创建AOP代理,这样就可以为任何接口实例创建代理了。
当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理,也可强制使用CGLIB。
AOP编程其实是很简单的事情,纵观AOP编程,程序员只需要参与三个部分:
- 定义普通业务组件;
- 定义切入点,一个切入点可能横切多个业务组件;
- 定义增强处理,增强处理就是在AOP框架为普通业务组件织入的处理动作
所以进行AOP编程的关键就是定义切入点和定义增强处理,一旦定义了合适的切入点和增强处理,AOP框架将自动生成AOP代理,即:代理对象的方法 = 增强处理+被代理对象的方法。
二、AOP的实现
1. 使用Maven创建Java Project,修改pom.xml
<properties>
<!-- JDK版本 -->
<java.version>1.8</java.version>
<!-- spring版本 -->
<spring.version>4.1.6.RELEASE</spring.version>
</properties>
<dependencies>
<!-- 核心容器 之 spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 核心容器 之 spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 核心容器 之 spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 核心容器 之 spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 核心容器 之 spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- aop额外需要aopalliance和aspectjweaver两个jar,aopalliance在引入spring-core时已自动导入 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.14</version>
</dependency>
</dependencies>
2. 编写普通业务组件
/**
* 普通业务组件
*/
public class CustomerBusiness { private String message; public void setMessage(String message) {
this.message = message;
} public void getMessage() {
System.out.println("Your Message : " + message);
}
}
3. 编写切面类,并配置applicationContext.xml文件
/**
* 切面
*/
public class TimeHandler {
public void printTime() {
System.out.println("CurrentTime = " + System.currentTimeMillis());
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="custBusi" class="com.linhw.demo.spring.CustomerBusiness">
<property name="message" value="Hello World!" />
</bean>
<bean id="timeHandler" class="com.linhw.demo.spring.aspect.TimeHandler" /> <!-- aop配置 -->
<aop:config>
<!-- 切面配置 -->
<aop:aspect id="time" ref="timeHandler">
<!-- 切入点配置,指明要拦截的方法 -->
<aop:pointcut id="addAllMethod" expression="execution(* com.linhw.demo.spring.CustomerBusiness.*())"/>
<!-- 前置通知 -->
<aop:before method="printTime" pointcut-ref="addAllMethod" />
<!-- 后置通知 -->
<aop:after method="printTime" pointcut-ref="addAllMethod" />
</aop:aspect> </aop:config> </beans>
在<aop:config>标签中有一个叫做proxy-target-class的属性,该属性决定是基于接口的还是基于类的代理被创建。
proxy-target-class="true"是基于类的代理将起作用(需要cglib库),
proxy-target-class="false"或者省略这个属性,则标准的JDK 基于接口的代理将起作用。
proxy-target-class在spring事务、aop、缓存这几块都有设置,其作用都是一样的。
附:使用注解实现aop
(1) 修改切面类
/**
* 切面
*/
@Aspect //声明式一个切面组件
@Component //加入到IoC容器
public class TimeHandler { //指定切入点表达式,拦截那些方法,即为哪些类生成代理对象
@Pointcut("execution(* com.linhw.demo.spring.CustomerBusiness.*())")
public void pointCut(){ } @Before("pointCut()")
public void printBeforeTime() {
System.out.println("Before CurrentTime = " + System.currentTimeMillis());
} @After("pointCut()")
public void prinAftertTime() {
System.out.println("After CurrentTime = " + System.currentTimeMillis());
} }
(2) 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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "> <bean id="custBusi" class="com.linhw.demo.spring.CustomerBusiness">
<property name="message" value="Hello World!" />
</bean> <!-- 必须开启aop注解方式,默认为false -->
<aop:aspectj-autoproxy/> <!-- 扫描切面类所在的包路径 -->
<context:component-scan base-package="com.linhw.demo.spring.aspect"/> </beans>
【Spring AOP】AOP的实现(三)的更多相关文章
- 10 Spring框架 AOP (三) Spring对AspectJ的整合
上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- (三)Spring 之AOP 详解
第一节:AOP 简介 AOP 简介:百度百科: 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个 ...
- 三、spring的AOP
AOP的基本认识 Aspect Oriented Programming,面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 利用AOP可以对业务逻辑的各个部分进行隔离,从而 ...
- Spring AOP及事务配置三种模式详解
Spring AOP简述 Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强. 使用场景如:日志打印.权限.事务控制等. 默认情况下,Spring会根据被代理的 ...
- spring学习三:Spring的Aop、代理
ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语: Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ...
- Spring基于AOP的事务管理
Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...
- spring的AOP
最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...
- Spring配置AOP实现定义切入点和织入增强
XML里的id=””记得全小写 经过AOP的配置后,可以切入日志功能.访问切入.事务管理.性能监测等功能. 首先实现这个织入增强需要的jar包,除了常用的 com.springsource.org.a ...
随机推荐
- 移动端(手机端)页面自适应解决方案1(rem布局)---750设计稿
设计稿尺寸为750 * 1340.结合网易.淘宝移动端首页html元素上的动态font-size属性.设计稿尺寸.前端与设计之间协作流程一般分为下面两种: 网易做法: 页面开头处引入下面这段代码,用于 ...
- Bliss OS 12.1下载 PC上Android10体验
下载也不是一帆风顺啊 这是设计者的secret: https://forum.xda-developers.com/android/software/bliss-os-x86-pc-s-12-x-de ...
- SQL Server 迁移数据库 (四)备份和还原
1. 备份 2. 复制 3. 粘贴 4. 还原 截图软件出问题了,估计重启下就好,但是备份还原比较简单,懂的都懂,马上下班了就不贴图了.
- 【CodeChef】August Challenge 2019 Div2 解题报告
点此进入比赛 \(T1\):Football(点此看题面) 大致题意: 求\(max(20a_i-10b_i,0)\). 送分题不解释. #include<bits/stdc++.h> # ...
- Paper | Dynamic Residual Dense Network for Image Denoising
目录 1. 故事 2. 动机 3. 做法 3.1 DRDB 3.2 训练方法 4. 实验 发表于2019 Sensors.这篇文章的思想可能来源于2018 ECCV的SkipNet[11]. 没开源, ...
- 解惑:如何使用SecureCRT上传和下载文件、SecureFX乱码问题
解惑:如何使用SecureCRT上传和下载文件.SecureFX乱码问题 一.前言 很多时候在windows平台上访问Linux系统的比较好用的工具之一就是SecureCRT了,下面介绍一下这个软件的 ...
- Window权限维持(十):Netsh Helper DLL
Netsh是Windows实用程序,管理员可以使用它来执行与系统的网络配置有关的任务,并在基于主机的Windows防火墙上进行修改.可以通过使用DLL文件来扩展Netsh功能.此功能使红队可以使用此工 ...
- kali渗透综合靶机(五)--zico2靶机
kali渗透综合靶机(五)--zico2靶机 靶机地址:https://www.vulnhub.com/series/zico2,137/#modal210download 一.主机发现 1.netd ...
- 基于kafka_2.11-2.1.0实现的生产者和消费者代码样例
1.搭建部署好zookeeper集群和kafka集群,这里省略. 启动zk: bin/zkServer.sh start conf/zoo.cfg. 验证zk是否启动成功: bin/zkServer. ...
- 【JS】---4用JS获取地址栏参数方法
用JS获取地址栏参数方法 // 方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var reg = new Reg ...