参考自 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方式)的更多相关文章

  1. 十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)

    创建web项目,引入jar包 引入Spring配置文件

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

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

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

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

  4. Spring的AOP开发(基于AspectJ的XML方式)

    Spring的AOP的简介: AOP思想最早是由AOP联盟组织提出的.Spring是使用这种思想最好的框架 Spring的AOP有自己实现的方式(非常繁琐). Aspect是一个AOP的框架, Spr ...

  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. Spring _day02_IoC注解开发入门

    1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...

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

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

  9. 【spring 注解驱动开发】spring事务处理原理

    尚学堂spring 注解驱动开发学习笔记之 - 事务处理 事务处理 1.事务处理实现 实现步骤: * 声明式事务: * * 环境搭建: * 1.导入相关依赖 * 数据源.数据库驱动.Spring-jd ...

随机推荐

  1. LeetCode-两个结构分别遍历,然后合并

    今天做了leetcode67题,两个2进制数相加,回想了一下其实有很多这种类型的题,比如leetcode2两数相加. 在做这种题时我自己的思路就是先循环遍历一个短的,然后跳出循环,判断是哪个结束,再接 ...

  2. 第一册:lesson ninety-five。

    原文: tickets,please. Two return tickets to London please. What time will the next train leave? At nin ...

  3. MySql常用 join 详解

    虽然这类资料比较多....我觉得还是有必要记下来,新手可以看看吧...老司机可以一眼飘过那... 常用SQL JOINS方式 1.SELECT select_list FROM TABLEA A LE ...

  4. 程序猿制造Bug的根本原因竟然是....

    传说中: 「杀一个程序猿不需要用枪,改三次需求就可以了.」 而且, 「这竟然也是程序猿制造Bug的根本原因....」 ↓↓↓↓↓↓↓ #/原始需求/#   你去饭店,坐下来. “服务员,给我来份宫保鸡 ...

  5. Offer选择与总结

    今天是2015.11.23,我估计这也是继高考.保研这些决定与选择之后,又一个比较重大的人生选择.最终选择了去微信支付,按钱来说比最高的offer少五万,其实挺心疼的.但是从发展和部门核心程度来讲,应 ...

  6. CSS3效果:animate实现点点点loading动画效果(二)

    box-shadow实现的打点效果 简介 box-shadow理论上可以生成任意的图形效果,当然也就可以实现点点点的loading效果了. 实现原理 html代码,首先需要写如下html代码以及cla ...

  7. 一个JVM进程启动后里面有几个线程

    在写Java程序时,通常我们管只有一个main函数(而没有别的Thread或Runnable的程序)叫单线程程序.但是我们写的这个所谓的单线程程序只是JVM这个程序中的一个线程,JVM本身是一个多线程 ...

  8. 2.Odoo产品分析 (一) – 一切为零

    查看Odoo产品分析系列--目录 1. 默认数据库 声明在先  本系列文档(Odoo产品分析)整理来自本人对该ERP的理解,并结合文档Working-with-Odoo-10-Second-Editi ...

  9. 手动编译websocket-sharp项目使其支持.net core

    以前项目中使用了websocket-sharp,挺好用.可惜,不支持.net core.好在手动编译很顺利: 从github下载源代码 创建dotnet core的类库,复制代码后并编译即可 dotn ...

  10. 短连接、长连接与keep-alive

    短连接与长连接 通俗来讲,浏览器和服务器每进行一次通信,就建立一次连接,任务结束就中断连接,即短连接.相反地,假如通信结束(如完成了某个HTML文件的信息获取)后保持连接则为长连接.在HTTP/1.0 ...