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的配置的更多相关文章

  1. Spring学习--基于 XML 的配置声明切面

    正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...

  2. (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置

    要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...

  3. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  4. 解释基于XML Schema方式的切面实现?

    在这种情况下,切面由常规类以及基于XML的配置实现.

  5. 解释基于 XML Schema 方式的切面实现?

    在这种情况下,切面由常规类以及基于 XML 的配置实现.

  6. MyBatis框架基于XML的配置

    什么是MyBatis? 答:它是一个持久层框架 说的太简单了吗?那让我们来看一下官方的文档描述: MyBatis有什么作用呢? 1.持久层的零实现 2.可以自动将数据封装到对象里面不需要手工编写映射的 ...

  7. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

  8. Spring学习记录(十三)---基于xml文件配置AOP

    上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP 其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行. 例子:这里的例子和上一篇的例子一样.换成xml方式 / ...

  9. idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题

    一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...

随机推荐

  1. 孤荷凌寒自学python第五十八天成功使用python来连接上远端MongoDb数据库

    孤荷凌寒自学python第五十八天成功使用python来连接上远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第四天.今天的感觉是,mongoDB数据 ...

  2. CSU-1989 赶路的小X

    题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1989 题目 Description A国一共有N座城市,由M条双向公路连 ...

  3. Windows 下开发.NET Core应用

    一.使用Visual Studio 2015开发1.1 依次安装Visual Studio 2015 Update 3.NET Core 1.0.0 - VS 2015 Tooling Preview ...

  4. hadoop-eclipse环境搭建(二)

    Eclipse插件配置 第一步:把我们的"hadoop-eclipse-plugin-1.0.0.jar"放到Eclipse的目录的"plugins"中,然后重 ...

  5. node.js开发hello world

    在你的 D 盘下面创建一个 server.js,写入以下内容 ---------------------------------------------------- var http = requi ...

  6. 用canvas实现鼠标拖动绘制矩形框

    需要用到jCanvas插件和jQuery. jCanvas下载:https://raw.githubusercontent.com/caleb531/jcanvas/master/jcanvas.mi ...

  7. POJ 2243 [SDOI2011]染色 | 树链剖分+线段树

    原题链接 肯定是树链剖分的题啦 树剖怎么做可以看我上一篇博客 如果我们已经剖完了: 然后考虑怎么维护重链和查询 用线段树维护的时候当前区间的区间颜色个数应该等于左儿子+右儿子,但是当左儿子的右端点和右 ...

  8. position:absolute和float隐式改变display为inline-block

    不论之前是什么类型的元素(display:none除外), 只要设置了position:absolute或float, 都会让元素以display:inline-block的方式显示, 可以设置长宽, ...

  9. 汕头市队赛SRM14 T3覆盖

    我们可以考虑两种情况 区间之间不相重叠 和 重叠 f[i][j]表示以当前最后一个区间以 i 结尾 并且选了 j 个区间 不相重叠的话 只要选 1-i-w 的max再加上 包含i在内的前四个数的和 相 ...

  10. cms .net webform去服务器控件标签化 pagebase新版本

    这是最近在干一个webform的cms的时候用起来的,原来虽然做过很多技术,什么remoting,wcf,webservice,可是弄来弄去,最后也没个收藏的地儿,全都放在笔记本儿上了,可是人又懒地可 ...