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文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...
随机推荐
- linux备忘录-bash
知识点 环境变量 HOME PATH MAIL SHELL RANDOM // 0~32767的随机数 declare -i number=$RANDOM*10/32768 //0-9的随机数 HIS ...
- Codeforces Round #329(Div2)
CodeForces 593A 题意:n个字符串,选一些字符串,在这些字符串中使得不同字母最多有两个,求满足这个条件可选得的最多字母个数. 思路:用c[i][j]统计文章中只有i,j对应两个字母出现的 ...
- 删除maven仓库中的lastUpdate文件
使用idea时导入hibernate 5.1.0的jar包,然后发现本地仓库中找不到该版本的jar 然后手贱 alt+enter 发现提示 update maven indices 然后以为更新就会好 ...
- nyoj 题目737 合并石子(一)
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的 ...
- hdu1576逆元的一道水题
hdu 1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). ...
- echarts异步加载
echarts体积很大,在移动端使用异步加载是一种提高渲染速度的方法,结合webpack的做法如下: require.ensure([], function(require){ const echar ...
- 公路建设 (highway.c/cpp/pas)
2.公路建设 (highway.c/cpp/pas) 在滨海市一共有 n 个城市,编号依次为 1 到 n,它们之间计划修建 m 条双向道路,其中 修建第 i 条道路的费用为 ci. 海霸王作为滨海市公 ...
- 【CZY选讲·黑白染色】
题目描述 给出平面上n 个点,试将他们黑白染色,要求染色后无法用一条直线把黑白完全分开. 随便输出一种方案. 数据范围 n<=100000 题解: ①点数很多,但是可以发现至多需 ...
- firefox解决flash崩溃
1.地址栏输入 about:config 2.查找dom.ipc.plugins.flash.subprocess.crashreporter.enabled 更改为false
- C++虚函数表解析(基础篇)
原文:http://blog.csdn.net/haoel/article/details/1948051 一.简介 C++中的虚函数的作用主要是实现了多态的机制.虚函数(Virtual Funct ...
