SpringAop名词解释+基于xml的配置
1,AOP名词解释
2,AOP演示
(1)导包:
(2)准备目标对象
package com.songyan.service; import org.aspectj.lang.ProceedingJoinPoint; import sun.net.www.content.text.plain; /**
* 通知类
* @author sy
*
*/
public class MyAdvice {
//前置通知-->目标方法调用前调用
//后置通知(如果发生异常不在调用)-->目标方法调用后调用
//环绕通知-->目标方法调用前和后调用
//异常拦截通知-->出现异常调用
//后置通知(不论是否发生异常都会调用)-->目标方法调用后调用
/*-----------------------前置--------------------------*/
public void before()
{
System.out.println("before~~~~~~");
} /*-----------------------后置(如果发生异常不在调用)--------------------------*/
public void after()
{
System.out.println("after(如果发生异常不在调用)~~~~~~");
} /*-----------------------环绕--------------------------*/
public Object around(ProceedingJoinPoint joinPoint) throws Throwable
{
System.out.println("around(上)~~~~~~");
//调用目标代码
Object proceed=joinPoint.proceed();
System.out.println("around(下)~~~~~~");
return proceed;
} /*-----------------------异常拦截--------------------------*/
public void afterException()
{
System.out.println("afterException~~~~~~");
} /*-----------------------后置(不论是否发生异常都会调用)--------------------------*/
public void after_final()
{
System.out.println("after(不论是否发生异常都会调用)~~~~~~");
} }
(3)准备通知(事物管理的代码)
S1:导入AOP命名空间
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> </beans>
S2:配置目标对象
<!--配置目标对象 -->
<bean name="userservice" class="com.songyan.service.UsrServiceImpl"></bean>
S3:配置通知对象
<!--配置通知对象 -->
<bean name="myadvice" class="com.songyan.service.MyAdvice"></bean>
S4:将通知织入目标对象
<!--将通知对象织入目标对象 -->
<aop:config>
<!--设置切入点 -->
<!--public void com.songyan.service.UsrServiceImpl.save()
void com.songyan.service.UsrServiceImpl.save()
* com.songyan.service.UsrServiceImpl.save()
* com.songyan.service.UsrServiceImpl.*()
* com.songyan.service.*ServiceImpl.*()
* com.songyan.service.*ServiceImpl.*(..)
* com.songyan.service..*ServiceImpl.*(..) -->
<aop:pointcut expression="* com.songyan.service..*ServiceImpl.*(..)"
id="pc" />
(4)配置进行织入(通知织入目标对象)
<!--给通知 设置切面 -->
<aop:aspect ref="myadvice">
<aop:before method="before" pointcut-ref="pc" />
<aop:after-returning method="after" pointcut-ref="pc" />
<aop:around method="around" pointcut-ref="pc" />
<aop:after-throwing method="afterException" pointcut-ref="pc" />
<aop:after method="after_final" pointcut-ref="pc" />
</aop:aspect>
</aop:config>
</beans>
完整配置信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!--配置目标对象 -->
<bean name="userservice" class="com.songyan.service.UsrServiceImpl"></bean>
<!--配置通知对象 -->
<bean name="myadvice" class="com.songyan.service.MyAdvice"></bean>
<!--将通知对象织入目标对象 -->
<aop:config>
<!--设置切入点 -->
<!--public void com.songyan.service.UsrServiceImpl.save() void com.songyan.service.UsrServiceImpl.save()
* com.songyan.service.UsrServiceImpl.save() * com.songyan.service.UsrServiceImpl.*()
* com.songyan.service.*ServiceImpl.*() * com.songyan.service.*ServiceImpl.*(..)
* com.songyan.service..*ServiceImpl.*(..) -->
<aop:pointcut expression="execution(* com.songyan.service.*ServiceImpl.*(..))" id="pc" />
<!--给通知 设置切面 -->
<aop:aspect ref="myadvice">
<aop:before method="before" pointcut-ref="pc" />
<aop:after-returning method="after" pointcut-ref="pc" />
<aop:around method="around" pointcut-ref="pc" />
<aop:after-throwing method="afterException" pointcut-ref="pc" />
<aop:after method="after_final" pointcut-ref="pc" />
</aop:aspect>
</aop:config>
</beans>
(4)测试
package com.songyan.service; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/service/beans1.xml");
UserService userService=(UserService)applicationContext.getBean("userservice");
userService.delete();
}
}
将delete里面的代码修改:
public void before()
{
System.out.println("before~~~~~~");
}
这样在运行的时候就会发生异常,
after-returning 定义的方法就不会运行
after-throwing 定义的方法就会运行
after 不管什么情况都会运行
SpringAop名词解释+基于xml的配置的更多相关文章
- Spring学习--基于 XML 的配置声明切面
正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...
- (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置
要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- 解释基于XML Schema方式的切面实现?
在这种情况下,切面由常规类以及基于XML的配置实现.
- 解释基于 XML Schema 方式的切面实现?
在这种情况下,切面由常规类以及基于 XML 的配置实现.
- MyBatis框架基于XML的配置
什么是MyBatis? 答:它是一个持久层框架 说的太简单了吗?那让我们来看一下官方的文档描述: MyBatis有什么作用呢? 1.持久层的零实现 2.可以自动将数据封装到对象里面不需要手工编写映射的 ...
- spring-AOP框架(基于AspectJ注解配置AOP)
基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...
- Spring学习记录(十三)---基于xml文件配置AOP
上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP 其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行. 例子:这里的例子和上一篇的例子一样.换成xml方式 / ...
- idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题
一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...
随机推荐
- 动态删边SPFA: [HNOI2014]道路堵塞
[HNOI2014]道路堵塞 题目描述 $A$ 国有 $N$座城市,依次标为$1$到$N$.同时,在这$N$座城市间有$M$条单向道路,每条道路的长度是一个正整数.现在,$A$国交通部指定了一条从城市 ...
- Error “can't use subversion command line client : svn” Probably the path to Subversion executable is wrong
错误提示如图. 大概意思就是SVN路径不对 解决方法如下: 首先下载Subversion 1.8.13(1.8) 下载链接(https://www.visualsvn.com/downloads/) ...
- vim使用的一些积累
vi visual interfacevim vi improved vim模式:编辑模式(命令模式)输入模式末行模式 编辑模式下,zz保存并退出移动光标:(编辑模式)1.逐字符移动 h 左 l 右 ...
- 第二节 PHPUnit测试的剖析
现在,让我们仔细看看测试结构的样子. 让我们从一个简单的测试用例开始,它将显示基本的PHPUnit测试结构. 以下代码片段是测试用于排序数组的两个PHP函数的一个非常基本的示例:asort()用于对数 ...
- VS2017+EF+Mysql生成实体数据模型(解决闪退的坑)
原文:VS2017+EF+Mysql生成实体数据模型(解决闪退的坑) 最近要使用VS2017+EF+Mysql,在生成实体数据模型踏过一些坑,在此做个总结. 1.先下载并安装 mysql-connec ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 L: 大整数减法
http://acm.ocrosoft.com/problem.php?cid=1316&pid=11 题目描述 求两个大的正整数相减的差. 输入 共2行,第1行是被减数a,第2行是减数b ...
- (总结)Nginx使用的php-fpm的两种进程管理方式及优化
PS:前段时间配置php-fpm的时候,无意中发现原来它还有两种进程管理方式.与Apache类似,它的进程数也是可以根据设置分为动态和静态的. php-fpm目前主要又两个分支,分别对应于php-5. ...
- 一些需要注意的ts
写了一段时间ts,在从头学习一遍,温故而之新 ts的一些技巧 1.巧用注释 通过/** */形式的注释可以给 TS 类型做标记,编辑器会有更好的提示: /** A cool guy. */ inter ...
- 【bzoj4884】[Lydsy2017年5月月赛]太空猫 dp
原文地址:http://www.cnblogs.com/GXZlegend/p/6825431.html 题目描述 太空猫(SpaceCat)是一款画面精致.玩法有趣的休闲游戏,你需要控制一只坐在迷你 ...
- 点对点协议(Point-to-Point Protocol)
简介 点对点协议简称PPP协议,工作在数据链路层.设计目的主要是用来通过拨号或专线方式建立点对点连接发送数据,使其成为各种主机. 网桥和路由器之间简单连接的一种共通的解决方案. PPP协议的组成 建立 ...