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. winform-实现类似QQ停靠桌面上边缘隐藏的效果

    //实现类似QQ停靠桌面上边缘隐藏的效果! private void timer1_Tick(object sender, EventArgs e) { System.Drawing.Point pp ...

  2. Codeforces 1088E 树形dp+思维

    比赛的时候看到题意没多想就放弃了.结果最后D也没做出来,还掉分了,所以还是题目做的太少,人太菜. 回到正题: 题意:一棵树,点带权值,然后求k个子连通块,使得k个连通块内所有的点权值相加作为分子除以k ...

  3. fragment中的WebView返回上一页

    public final class Text1Fm extends Fragment { static WebView mWeb; private View mContentView; privat ...

  4. Linux 内核数据结构bitmap

    #include <stdio.h> #include <stdlib.h> #define MAX_PRIO 10000 #define BITS_PER_LONG 32 # ...

  5. POJ2374 Fence Obstacle Course 【线段树】

    题目链接 POJ2374 题解 题意: 给出\(n\)个平行于\(x\)轴的栅栏,求从一侧栅栏的某个位置出发,绕过所有栅栏到达另一侧\(x = 0\)位置的最短水平距离 往上说都是线段树优化dp 我写 ...

  6. 用iFrame模拟Ajax上传文件

    前段时间在解决ajax上传文件时折腾了好一阵.直接用$.post上传文本信息肯定是没有问题的.但是$.post直接上传图片是不可行的. 后来看到网上的一些解决方案,有现成的ajax上传文件的封装的方法 ...

  7. ACM-渊子赛马

    题目: 赛马是一古老的游戏,早在公元前四世纪的中国,处在诸侯割据的状态,历史上称为“战国时期”.在魏国作官的孙膑,因为受到同僚庞涓的迫害,被齐国使臣救出后,到达齐国国都. 赛马是当时最受齐国贵族欢迎的 ...

  8. Windows转移FSMO角色

    转移 FSMO 角色若要使用 Ntdsutil 实用工具转移 FSMO 角色,请按照下列步骤操作:1.登录到基于 Windows 2000 Server 或基于 Windows Server 2003 ...

  9. Linux环境准备20160921

    这篇文章,是我准备linux的java环境时候,碰到的各种问题,采用的是centos 6.5版本. 1.卸载open jdk  先查看 rpm -qa | grep java  # java-1.4. ...

  10. Lesson 7: C#多线程

    C#多线程 1.适用于: 通过网络进行通信 执行占用时间的操作 区分具有不同优先级的任务 使用户界面在执行后台任务时能快速响应用户的交互 2.Thread类常用属性及方法 属性: IsAlive:显示 ...