一、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的实现(三)的更多相关文章

  1. 10 Spring框架 AOP (三) Spring对AspectJ的整合

    上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...

  2. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  3. (三)Spring 之AOP 详解

    第一节:AOP 简介 AOP 简介:百度百科: 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个 ...

  4. 三、spring的AOP

    AOP的基本认识 Aspect Oriented Programming,面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 利用AOP可以对业务逻辑的各个部分进行隔离,从而 ...

  5. Spring AOP及事务配置三种模式详解

    Spring AOP简述 Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强. 使用场景如:日志打印.权限.事务控制等. 默认情况下,Spring会根据被代理的 ...

  6. spring学习三:Spring的Aop、代理

    ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语:    Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ...

  7. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

  8. spring的AOP

    最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...

  9. Spring配置AOP实现定义切入点和织入增强

    XML里的id=””记得全小写 经过AOP的配置后,可以切入日志功能.访问切入.事务管理.性能监测等功能. 首先实现这个织入增强需要的jar包,除了常用的 com.springsource.org.a ...

随机推荐

  1. linux 安装盘作为 repo

    1) CentOS 7.7 安装完之后, /etc/yum.repos.d 下面有很多.repo. 其中有一个CentOS-Media.repo. 编辑文件把enabled 改成 1 . 然后把其他. ...

  2. PHP输出函数

    1.print()输出 header('Content-Type:text/html;charset=utf-8'); print ("最近想学习PHP,大家推荐哪个学校好点?/n" ...

  3. SQL 错误: ORA-65096: 公用用户名或角色名无效 65096. 00000 - "invalid common user or role name" *Cause: An attempt was made to create a common user or role with a name

    在Oracle SQL Developer中,试图创建RD用户时,出现了如下的错误: 在行: 上开始执行命令时出错 - 错误报告 - SQL 错误: ORA: 公用用户名或角色名无效 . - &quo ...

  4. 为Azure DevOps Server (TFS) 配置安全访问(HTTPS with SSL)

    Contents 1. 概述 2. HTTP和HTTS比较 支持HTTP和HTTPS两种方式 要求所有连接使用HTTPS 优点: 缺点: 3. 为Azure DevOps Server 配置安全访问 ...

  5. Qt 编译配置相关总结

    MinGW 与 MSVC 编译的区别 我们可以从 Qt 下载页面看到两种版本编译器,如下图: 我们来对比一下这两个编译器的区别: MSVC 是指微软的 VC 编译器. MinGW 是 Minimali ...

  6. 解决 cannot find reference 'LSHForest' in '__init__.py'

    from sklearn.neighbors import LSHForest cannot find reference 'LSHForest' in '__init__.py'报错 pip3 li ...

  7. 海边拾贝-B-优秀博客/网站

    记下若干优秀博客,方便后期检索.会不定期更新: 优秀的程序员,从使用Github开始:https://help.github.com/en/github/managing-your-work-on-g ...

  8. Vue全选和全不选

    HTML代码: <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> ...

  9. Vue.js 源码分析(二十五) 高级应用 插槽 详解

    我们定义一个组件的时候,可以在组件的某个节点内预留一个位置,当父组件调用该组件的时候可以指定该位置具体的内容,这就是插槽的用法,子组件模板可以通过slot标签(插槽)规定对应的内容放置在哪里,比如: ...

  10. kali渗透综合靶机(十六)--evilscience靶机

    kali渗透综合靶机(十六)--evilscience靶机 一.主机发现 1.netdiscover -i eth0 -r 192.168.10.0/24 二.端口扫描 1. masscan --ra ...