Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)
参考自 https://www.cnblogs.com/ltfxy/p/9882430.html
创建web项目,引入jar包
除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个jar包:
附jar包链接:链接:https://pan.baidu.com/s/1E_8NA-DcWwt9hdK-czkm9A 提取码:xq8n
除此之外,Spring要整合Junit单元测试的话,还需引入
引入Spring配置文件
引入aop开发的约束
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===============================引入aop开发的约束============================ -->
<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">
</beans>
编写目标类与实现类,并完成配置
package spring.day2_aop; public interface ProductDao {
public void find();
public void save();
public void delete();
public void update();
}
package spring.day2_aop; public class ProductDaoImp implements ProductDao { @Override
public void find() {
System.out.println("查询商品。。。");
} @Override
public void save() {
System.out.println("保存商品。。。"); } @Override
public void delete() {
System.out.println("删除商品。。。"); } @Override
public void update() {
System.out.println("修改商品。。。"); } }
编写测试类
package spring.day2_aop; 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:applicationContext4.xml")
public class SpringDemo1 { @Resource(name="productDao")
private ProductDao productDao;
@Test
public void demo1() {
productDao.save();
productDao.delete();
productDao.update();
productDao.find();
}
}
运行结果如下:
编写一个切面类并通过aop配置产生代理
package spring.day2_aop;
/*
* 切面类:放入通知的方法即增强的那些方法
*/
public class MyAspectXml {
/*
* 权限校验的方法
*/
public void checkPri() {
System.out.println("权限校验..............");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===============================引入aop开发的约束============================ -->
<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="productDao" class="spring.day2_aop.ProductDaoImp"></bean> <!-- 将切面类交给spring管理 -->
<bean id="myAspect" class="spring.day2_aop.MyAspectXml"></bean> <!-- 通过aop的配置来对目标类产生代理 -->
<aop:config>
<!-- 配置切入点,表达式(execution函数)配置当前web项目下哪些类的哪些方法需要增强,*代表任意返回值,..代表任意参数 -->
<aop:pointcut
expression="execution(* spring.day2_aop.ProductDaoImp.save(..))"
id="pointcut1" />
<!-- 配置切面 -->
<aop:aspect ref="myAspect">
<!-- 配置为前置增强 -->
<aop:before method="checkPri" pointcut-ref="pointcut1" />
</aop:aspect>
</aop:config>
</beans>
测试运行
补充:
@Runwith() :用于指定junit运行环境。它是junit提供给其他框架测试用的。这个注解是指定测试的时候使用的执行类,不写的话会使用默认的执行类
@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境
@ContextConfiguration :Spring整合JUnit4测试时,使用注解引入多个配置文件
单个文件时:
@ContextConfiguration(Locations="classpath:applicationContext.xml")
多个文件时:可用{}
@ContextConfiguration(Locations = { "classpath:spring1.xml", "classpath:spring2.xml" })
Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)的更多相关文章
- 十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)
创建web项目,引入jar包 引入Spring配置文件
- 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)
[AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...
- 基于AspectJ的XML方式进行AOP开发
-------------------siwuxie095 基于 AspectJ 的 XML 方式进行 AOP 开发 1 ...
- Spring的AOP开发(基于AspectJ的XML方式)
Spring的AOP的简介: AOP思想最早是由AOP联盟组织提出的.Spring是使用这种思想最好的框架 Spring的AOP有自己实现的方式(非常繁琐). Aspect是一个AOP的框架, Spr ...
- Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)
1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...
- Spring事务管理之声明式事务管理-基于AspectJ的XML方式
© 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(AspectJ)管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( ` ...
- Spring _day02_IoC注解开发入门
1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...
- Spring的AOP基于AspectJ的注解方式开发3
上上偏博客介绍了@Aspect,@Before 上篇博客介绍了spring的AOP开发的注解通知类型:@Before,@AfterThrowing,@After,@AfterReturning,@Ar ...
- 【spring 注解驱动开发】spring事务处理原理
尚学堂spring 注解驱动开发学习笔记之 - 事务处理 事务处理 1.事务处理实现 实现步骤: * 声明式事务: * * 环境搭建: * 1.导入相关依赖 * 数据源.数据库驱动.Spring-jd ...
随机推荐
- 在.net中怎么解析json串 [Error reading JObject from JsonReader. Current JsonReader item is not an obj]
编辑时间:2017-05-10,增加一种转化list的方法 一.以前知道一种解析json串的方法,觉得有点麻烦.就从别的地方搜到了另一种 string json = vlt.getlist(); JO ...
- IIS域名转发
在IIS中设置Http重定向 界面操作如下: 最终通过上面的操作生成了一个配置文件如下: 我这面就是一个空的目录,里面仅包含这个配置文件,就可以实现转发啦
- Java学习笔记之——方法重载
方法重载: overload 1. 方法签名 组成:方法名(参数列表) 参数列表不同分为哪些情况? (1)有无参数 (2)参数的类型 (3)参数的个数 (4)参数的顺序(前提是类型不一样) 2.方法重 ...
- 【Spring】22、Spring缓存注解@Cache使用
缓存注解有以下三个: @Cacheable @CacheEvict @CachePut @Cacheable(value=”accountCache”),这个注释的意思是,当调用这个 ...
- C#设计模式之六适配器模式(Adapter Pattern)【结构型】
一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题,那[ ...
- input属性为number时,如何去掉+、-号?
直接上答案 <style> input[type='number']{-moz-appearance:textfield;} input[type=number]::-webkit-inn ...
- [转载]Web Service到底是什么
转自:http://blog.csdn.net/wooshn/article/details/8069087/ 武僧的专栏 一.序言 大家或多或少都听过WebService(Web服务),有一段时间 ...
- C#基础(201)--常量枚举
本文知识点: 1.掌握常量的定义和使用方法 2.理解枚举的作用和特点 3.掌握枚举的使用方法 1.1.常量的定义语法 const 数据类型 常量名称 = 值: 1.2.常见错误 1.3常量的 ...
- spring-boot-starter-thymeleaf对没有结束符的HTML5标签解析出错
springboot 在使用thymeleaf 作为模板时,当出现未关闭标签时,如下所示代码,标签没有关闭. <link href="plugin/layui/css/layui.cs ...
- 30.Odoo产品分析 (四) – 工具板块(2) – 搜索和仪表盘(2)
查看Odoo产品分析系列--目录 在前面的模块中,简单介绍过了odoo如何搜索系统中的各种数据集,并保存这些过滤器,以便在之后需要时能够轻松访问这些过滤器.这里将做更详细的介绍.最后分析仪表盘的功能, ...