AspectJ的六种通知的类型,最后一种不讲,只讲前五种.

 环绕通知是可以阻止目标方法执行的.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引AOP的约束了,Schema里面必须是带有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">
<!-- 自动的生成代理 底层就是AnnotationAwareAspectJAutoProxyCreator-->
<aop:aspectj-autoproxy />
<bean id="userDao" class="cn.itcast.spring3.demo1.UserDao"></bean> <bean id="myAspect" class="cn.itcast.spring3.demo1.MyAspect"></bean> </beans>
package cn.itcast.spring3.demo1;

public class UserDao {
public void add(){
System.out.println("添加用户");
}
//public void update(){
public int update(){
System.out.println("修改用户");
return 1;
}
public void delete(){
System.out.println("删除用户");
}
public void find(){
//int i = 1/0;
System.out.println("查询用户");
}
}
package cn.itcast.spring3.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
@Test
public void demo1(){
userDao.add();
userDao.delete();
userDao.update();
userDao.find();
}
}
package cn.itcast.spring3.demo1;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; //我的切面类
/**
* 切面类:就是切点与增强的结合
* 切点就是哪些方法要使用什么增强
* @author zhongzh
*
*/
@Aspect //用来定义切面
public class MyAspect {
//写增强的代码,假设现在做的是一个前置增强.
@Before(value="execution(* cn.itcast.spring3.demo1.UserDao.add(..))") //前置增强的注解是Before 刚一写完Before就报错了,它里面肯定是有属性没写的.学过JDK的注解,如果只想使用它里面的value属性,value是可以不写的.
//现在只对UserDao.add()应用前置增强.
public void before(JoinPoint joinPoint){//JointPoint是可以用作增强的一个点.就是被增强的那个方法的描述.
//System.out.println("前置增强......");
System.out.println("前置增强......"+joinPoint);
}
@AfterReturning(value="execution(* cn.itcast.spring3.demo1.UserDao.update(..))",returning="returnVal")//这里也是要写切面表达式
//public void afterReturn(){
public void afterReturn(Object returnVal){
System.out.println("后置增强....方法的返回值:"+returnVal);
}
@Around(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕前增强.....");
Object obj = proceedingJoinPoint.proceed();
System.out.println("环绕后增强.....");
return obj;//如果想要阻止目标方法的执行这里可以return null
}
@AfterThrowing(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("不好了 出异常了!!!!"+e.getMessage());
}
@After(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))") //最终通知 类似于try catch finally里面的那个finally
public void after(){
System.out.println("最终通知....");//最终通知它不管你什么情况下都会执行
} }

day39 09-Spring的AOP:基于AspectJ的通知类型的更多相关文章

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

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

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

    参考自黑马培训机构 上一篇博客提到了在配置文件中开启aop的注解开发,以及简单使用了@Before,@Aspect 这是为了告诉spring为前置通知和切面类 接下来介绍aop的注解的通知类型,和切入 ...

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

    参考自黑马培训机构 创建项目,引入jar包 编写目标类,切面类并完成配置 package spring.day2_aop2; /* * 编写目标类 */ public class OrderDao { ...

  4. 十五 Spring的AOP的注解的通知类型,切入点的注解

    Spring的注解的AOP的通知类型 @Before:前置通知 @AfterReturning:后置通知 @Around:环绕通知 @AfterThrowing:异常抛出通知 @After:最终通知 ...

  5. spring(二) AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

  6. Spring AOP 基于AspectJ

    简介 AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持.因为Spring1.0的时候Aspectj还未出现; AspectJ1.5中新增了对 ...

  7. Spring 使用AOP——基于注解配置

    首先,使用注解实现AOP是基于AspectJ方式的. 创建包含切点方法的类 package cn.ganlixin.test; import org.aspectj.lang.annotation.P ...

  8. 09 Spring框架 AOP (二) 高级用法

    上一篇文章我们主要讲了一点关于AOP编程,它的动态考虑程序的运行过程,和Spring中AOP的应用,前置通知,后置通知,环绕通知和异常通知,这些都是Spring中AOP最简单的用法,也是最常用的东西, ...

  9. AOP——基于AspectJ的注解来实现AOP操作

    1.使用注解方式实现AOP操作 第一步:创建对象 <!-- 创建对象 --> <bean id="book" class="com.bjxb.aop.B ...

随机推荐

  1. sde中的shp数据无法编辑

    最近整理空间数据库时,用sde比较多,发现在编辑sde中的数据时总是出现数据被锁或者是被其他应用程序占用.用了很多方法处理,但不是每个方法都实用.下面讲的是我在删除shp或者给shp增加字段时所遇到的 ...

  2. Python导出DBF文件到Excel的方法

    Python导出DBF文件到Excel的方法 这篇文章主要介绍了Python导出DBF文件到Excel的方法,实例分析了Python基于win32com模块实现文件导出与转换的相关技巧,分享给大家供大 ...

  3. PAT甲级——A1055 The World's Richest

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  4. AIX系统搭建NFS服务器

    本文使用场景:aix6.1升级到aix7.1之后,需要打补丁aix7.1 TL4的补丁,补丁文件有将近10G,当多个系统都升级时,此时搭建nfs服务器,只需要一次上传,其余需升级系统作为客户端只需通过 ...

  5. Java程序员面试题收集(4)

    Java面试题和答案JAVA相关基础知识1.面向对象的特征有哪些方面      1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题 ...

  6. 部分树形DP的优化

    ural1018. Binary Apple Tree 题目大意 有一棵n个节点的树,树上每个节点有一个值,选择m个节点使这些节点值的和最大 要求:如果选当前节点,则必须选它的父节点 解法: 我们设d ...

  7. independent set 1

    independent set 1 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 102400K,其他语言204800K64bit IO Format: %lld 题目描述 Note: ...

  8. TZ_11_Spring-Boot的属性注入方式(jdbc为例)

    1.以上一篇文档为基础 2.创建jdbc外部属性文件 application.properties此名字为默认文件名在使用是不需要使用 @Propertysource("classpath: ...

  9. 这些Excel学会了,你做账的效率将大大提高

    这些Excel学会了,你做账的效率将大大提高 这些功能学会了,工作效率将大大提高. 1.excel的快速访问工具栏: 我的快速访问工具栏由左到右主要是"保存"."新建&q ...

  10. flask的基本操作

    常用的SQLAlchemy字段类型 # coding:utf-8 from flask import Flask from flask_sqlalchemy import SQLAlchemy app ...