Spring的AOP的简介:

AOP思想最早是由AOP联盟组织提出的。Spring是使用这种思想最好的框架

Spring的AOP有自己实现的方式(非常繁琐)。 Aspect是一个AOP的框架, Spring引入 Aspect作为自身AOP的开发。

Spring两套AOP开发方式

◆ Spring传统方式(弃用)。

◆ Spring基于 Aspect的AoP的开发(使用)

AOP 的开发中的相关术语:

废话少说,我们上代码!

1.引入相应的 jar 包

2.引入 Spring 的配置文件

在src下创建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"
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 definitions here --> </beans>

3.编写目标类

创建类和接口

package com.rick.aop.demo1;

public interface ProductDao {
public void save();
public void update();
public void find();
public void delete(); }
package com.rick.aop.demo1;

public class ProductDaoImpl implements ProductDao{

	@Override
public void save() {
System.out.println("商品添加。。。");
}
@Override
public void update() {
System.out.println("商品更新。。。");
}
@Override
public void find() {
System.out.println("商品查找。。。");
}
@Override
public void delete() {
System.out.println("商品删除。。。");
} }

4.目标类的配置

<!-- 配置目标对象:被增强的对象 -->
<bean id="productDao" class = "com.rick.aop.demo1.ProductDaoImpl"></bean>

5.整合 Junit 单元测试

引入 spring-test.jar

package com.rick.aop.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo { @Resource(name = "productDao")
private ProductDao productDao; @Test
public void demo1() {
productDao.save();
productDao.update();
productDao.delete();
productDao.find();
}
}

编写切面类

package com.rick.aop.demo1;

/*
* 切面类
*/
public class MyAspectXml {
//前置增强
public void checkPri() {
System.out.println("权限校验===========");
}
}

配置:将切面类交给spring管理

<!-- 配置切面类 -->
<bean id="myAspectXml" class = "com.rick.aop.demo1.MyAspectXml"></bean>
<!-- 进行 aop 的配置 -->
<aop:config>
<!-- 配置切入点表达式:哪些类的哪些方法需要进行增强 -->
<aop:pointcut expression="execution(*com.rick.aop.demo1.ProductDaoImpl.save(..))" id="pointcut1"/>
<!-- 配置切面 -->
<aop:aspect ref="myAspectXml">
<aop:before method="checkPri" pointcut-ref="pointcut1" />
</aop:aspect>
</aop:config>

测试:

通知类型

  • 前置通知可以获得切入点信息
  • 后置通知可以获得返回值
  • 环绕通知可以组织目标方法的 执行

切入点表达式

其他的增强的配置:

上代码

修改

测试

Spring的AOP开发(基于AspectJ的XML方式)的更多相关文章

  1. Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)

    参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...

  2. 基于AspectJ的XML方式进行AOP开发

    -------------------siwuxie095                                 基于 AspectJ 的 XML 方式进行 AOP 开发         1 ...

  3. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

  4. 十四 Spring的AOP的基于AspectJ的注解开发

    Spring的AOP的基于AspectJ的注解开发 创建项目,引入jar包 编写目标类.切面类 配置目标类.切面类 在注解文件里开启AOP的开发 <?xml version="1.0& ...

  5. Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)

    1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...

  6. Spring事务管理之声明式事务管理-基于AspectJ的XML方式

    © 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(AspectJ)管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( ` ...

  7. day39-Spring 11-Spring的AOP:基于AspectJ的XML配置方式

    package cn.itcast.spring3.demo2; import org.aspectj.lang.ProceedingJoinPoint; /** * 切面类 * @author zh ...

  8. Spring的AOP基于AspectJ的注解方式开发3

    上上偏博客介绍了@Aspect,@Before 上篇博客介绍了spring的AOP开发的注解通知类型:@Before,@AfterThrowing,@After,@AfterReturning,@Ar ...

  9. 基于AspectJ的注解方式进行AOP开发

    -------------------siwuxie095                                     基于 AspectJ 的注解方式进行 AOP 开发         ...

随机推荐

  1. lower_bound() 函数使用详解

    简介 lower_bound()函数是用来求一个容器中,第一个大于等于所要查找的元素的地址,具体的原理是二分查找,因此它只能用于非降序序列. 他有三个参数,第一个参数是容器的初始地址,第二个参数是容器 ...

  2. 什么是redis事务

    一.什么是redis事务? 可以一次性执行多条命令,本质上是一组命令的集合.一个事务中的所有命令都会序列化,然后按顺序地串行化执行,而不会被插入其他命令 二.Redis 事务可以做什么? 一个队列中, ...

  3. Java如何实现序列化,有什么意义?

    1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态,并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存Object States, 但是Java给你提供一种应该 ...

  4. Python 爬取 北京市政府首都之窗信件列表-[信息展示]

    日期:2020.01.25 博客期:133 星期六 [代码说明,如果要使用此页代码,必须在本博客页面评论区给予说明] //博客总体说明 1.准备工作 2.爬取工作 3.数据处理 4.信息展示(本期博客 ...

  5. python使用pip安装库时出现timeout或者速度慢

    豆瓣:https://pypi.doubanio.com/simple/ pip3 install -i https://pypi.doubanio.com/simple/ selenium easy ...

  6. 防火墙问题 Linux系统 /etc/sysconfig/路径下无iptables文件

    虚拟机新装了一个CentOs7,然后做防火墙配置的时候找不到iptables文件,解决方法如下: 因为默认使用的是firewall作为防火墙,把他停掉装个iptable systemctl stop ...

  7. titleView发生偏移、titleView与masonry、titleView的设置、titleView的使用

    navigationItem的titleView属性的设置本身是很简单的,容易出问题的原因是自动化布局与frame混用造成的. 本文一步一步的讲解,力求找到问题的起源.如果你也在这块同样遇到问题,不妨 ...

  8. Lesson 10 Silicon valley

    What does the computer industry thrive on apart from anarchy? Technology trends may push Silicon Val ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. rendering path定义了什么?有哪些?有什么作用?有什么限制?

    rendering path 定义了光照在一个shader pass中处理的对象与顺序,如逐像素处理,逐顶点处理,向前处理,后向 deferred用于处理不透明物体,会先把它们根据深度检测,过滤后的内 ...