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. [网络转载 ]LoadRunner技巧之THML与URL两种录制模式分析

    loadrunner自带网站的访问 Html_based script模式 Action() { web_url("WebTours", "URL=http://127. ...

  2. Socket通信实现步骤

    public class Server { public static void main(String[] args) { try { ServerSocket serverSocket = new ...

  3. python爬虫(二) urlparse和urlsplit函数

    urlparse和urlsplit函数: urlparse: url='http://www.baidu.com/s?wd=python&username=abc#1' result=pars ...

  4. 在Centos上安装Postgre SQL

    检查PostgreSQL 是否已经安装 rpm -qa | grep postgres 检查PostgreSQL 是否已经安装 rpm -qal | grep postgres 检查PostgreSQ ...

  5. alert \ confirm \ prompt

    alert() : 会将()中的内容弹出,返回的是()中的内容值,也就是字符串值 confirm :需要用户点击 "确定" 或 "取消" ,若用户点击 ”确定“ ...

  6. 设计模式课程 设计模式精讲 14-2 组合模式coding

    1 代码演练 1.1 代码演练1(组合模式1) 1.2 代码演练2(组合模式1之完善) 1 代码演练 1.1 代码演练1(组合模式1) 需求: 打印出木木网的课程结构, 我们用一个组建类作为接口,课程 ...

  7. Redis调用

    Redis帮助类 using StackExchange.Redis; using System; using System.Collections.Generic; using System.Lin ...

  8. iOS10打电话、发短信、发邮件等小功能

    注意:iOS10.0以后,使用openURL会有延迟,需要使用 openURL: options: completionHandler: 一.概要 本文中主要就是介绍在iOS中实现打电话.发短信.发邮 ...

  9. 吴裕雄--天生自然HADOOP操作实验学习笔记:协同过滤算法

    实验目的 初步认识推荐系统 学会用mapreduce实现复杂的算法 学会系统过滤算法的基本步骤 实验原理 前面我们说过了qq的好友推荐,其实推荐算法是所有机器学习算法中最重要.最基础.最复杂的算法,一 ...

  10. 用javaweb写一个注册界面,并将数据保存到后台数据库(全部完成)(课堂测试)

    一.题目:WEB界面链接数据库 1.考试要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母. ...