一、spring导包

2、目标对象

public class UserServiceImpl implements UserService {
@Override
public void save() {
System.out.println("保存用户!");
//int i = 1/0;
}
@Override
public void delete() {
System.out.println("删除用户!");
}
@Override
public void update() {
System.out.println("更新用户!");
}
@Override
public void find() {
System.out.println("查找用户!");
}
}

3、准备通知

//通知类
public class MyAdvice {
/**
* 前置通知
*-目标方法运行前调用
*后置通知(如果出现异常不会调用)
*-目标方法运行之后调用
*环绕通知
*-在目标方法之前之后调用
*异常拦截通知
*-在目标方法运行之后调用
*后置通知(无论是否出现异常都会调用)
*目标方法运行后调用
*/ //前置通知
public void before() {
System.out.println("这是前置通知!!!");
}
//后置通知通知
public void afterReturning() {
System.out.println("这是后置通知(如果出现异常不会调用)!!!");
}
//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知之前的部分");
Object proceed = pjp.proceed();//调用目标方法
System.out.println("这是环绕通知之后的部分");
return proceed;
}
//异常通知
public void afterException() {
System.out.println("出事了,出现异常了");
}
//后置通知
public void after() {
System.out.println("出事了,出现异常了");
} }

4、配置进行织入

<?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:p="http://www.springframework.org/schema/p"
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 "> <!-- 导入安aop(约束)命名空间 -->
<!-- 1.配置目标对象 -->
<bean name="userServiceTarget" class="cn.itcast.service.UserServiceImpl"></bean>
<!-- 2.配置通知对象 -->
<bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice"></bean>
<!-- 3.配置将通知织入目标对象 -->
<aop:config>
<!-- 配置切入点
书写expression="execution(* cn.itcast.service.*ServiceImpl.*(..))"
public void cn.itcast.service.UserServiceImpl.save()
一般 public省略掉 ,一般对返回值不做要求用*表示,类下的放大,用*表示全部的方法
* cn.itcast.service.UserServiceImpl.*()
继续演化..表示不对参数有任何要求
* cn.itcast.service.UserServiceImpl.*(..)
继续演化,不对集体的类有要求
* cn.itcast.service.*ServiceImpl.*(..)
继续演化 ,不只找service中的类而且找子包
* cn.itcast.service..*ServiceImpl.*(..)
-->

<aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pc"/>
<aop:after-returning method="afterReturning" pointcut-ref="pc"/>
<aop:around method="around" pointcut-ref="pc"/>
<aop:after method="after" pointcut-ref="pc"/>
<aop:after-throwing method="afterException" pointcut-ref="pc"/>
</aop:aspect> </aop:config> </beans>

测试:

/**
* @RunWith :帮我们创建容器
* @ContextConfiguration :指定创建容器时使用哪个配置文件
* @author zws
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/e_annotationaop/applicationContext.xml")
public class Demo { @Resource(name="userServiceTarget")
private UserService us; @Test
public void fun1(){
us.save();
} }

结果:

这是环绕通知之前的部分
这是前置通知!!!
保存用户!
这是环绕通知之后的部分
出事了,出现异常了
这是后置通知(如果出现异常不会调用)!!!

注解配置:

//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice { //前置通知
// -目标方法运行前调用
//后置通知(如果出现异常不会调用)
// -目标方法运行之后调用
//环绕通知
// -在目标方法之前之后调用
//异常拦截通知
// -在目标方法运行之后调用
//后置通知(无论是否出现异常都会调用)

//前置通知
@Before("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void before() {
System.out.println("这是前置通知!!!");
} //后置通知通知
@AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void afterReturning() {
System.out.println("这是后置通知(如果出现异常不会调用)!!!");
} //环绕通知
@Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知之前的部分");
Object proceed = pjp.proceed();//调用目标方法
System.out.println("这是环绕通知之后的部分");
return proceed;
}
//异常通知
@AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void afterException() {
System.out.println("出事了,出现异常了");
}
//后置通知
@After("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void after() {
System.out.println("出事了,出现异常了");
} }

配置:

<?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:p="http://www.springframework.org/schema/p"
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 "> <!-- 导入安aop(约束)命名空间 -->
<!-- 1.配置目标对象 -->
<bean name="userServiceTarget" class="cn.itcast.service.UserServiceImpl"></bean>
<!-- 2.配置通知对象 -->
<bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice"></bean>
<!-- 3.配置将通知织入目标对象 使用注解完成织如-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

测试:

/**
* @RunWith :帮我们创建容器
* @ContextConfiguration :指定创建容器时使用哪个配置文件
* @author zws
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/e_annotationaop/applicationContext.xml")
public class Demo { @Resource(name="userServiceTarget")
private UserService us; @Test
public void fun1(){
us.save();
} }

结果;

这是环绕通知之前的部分
这是前置通知!!!
保存用户!
这是环绕通知之后的部分
出事了,出现异常了
这是后置通知(如果出现异常不会调用)!!!
 

spring再学习之AOP实操的更多相关文章

  1. spring再学习之AOP事务

    spring中的事务 spring怎么操作事务的: 事务的转播行为: 事务代码转账操作如下: 接口: public interface AccountDao { //加钱 void addMoney( ...

  2. spring再学习之AOP准备

    一.aop思想: 横向重复,纵向抽取 1.乱码 2.事务管理 3,action 二.spring能够为容器中管理的对象生成代理对象 1.spring能帮我们生成代理对象 2.spring实现aop的原 ...

  3. spring再学习之设计模式

    今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ...

  4. Spring基础学习(四)—AOP

    一.AOP基础 1.基本需求      需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...

  5. Spring框架学习06——AOP底层实现原理

    在Java中有多种动态代理技术,如JDK.CGLIB.Javassist.ASM,其中最常用的动态代理技术是JDK和CGLIB. 1.JDK的动态代理 JDK动态代理是java.lang.reflec ...

  6. Spring框架学习05——AOP相关术语详解

    1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查 ...

  7. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  8. spring再学习之注解

    1.使用注解配置spring <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi= ...

  9. spring再学习之配置详解

    applicationContext.xml文件配置: bean元素: <?xml version="1.0" encoding="UTF-8"?> ...

随机推荐

  1. wmic process进程管理

    process    进程管理工具 示例:1.列举当前的进程.进程路径.命令行.进程ID.父进程ID.线程数,内存使用::wmic process get name,executablepath,co ...

  2. 《Go 语言并发之道》读后感 - 第四章

    <Go 语言并发之道>读后感-第四章 约束 约束可以减轻开发者的认知负担以便写出有更小临界区的并发代码.确保某一信息再并发过程中仅能被其中之一的进程进行访问.程序中通常存在两种可能的约束: ...

  3. 欢迎来到 ZooKeeper 动物世界

    本文作者:HelloGitHub-老荀 Hi,这里是 HelloGitHub 推出的 HelloZooKeeper 系列,免费有趣.入门级的 ZooKeeper 开源教程,面向有编程基础的新手. Zo ...

  4. windows上传ipa到苹果开发者中(app store)的方法

    假如你已经使用过苹果开发者中心上架app,你肯定知道在苹果开发者中心的web界面,无法直接提交ipa文件,而是需要使用第三方工具,将ipa文件上传到构建版本,开发者中心才能在构建版本里选择构建版本上架 ...

  5. 转 Fiddler1 简单使用

    Fiddler1 简单使用   文章转自:https://www.cnblogs.com/zhengna/p/9008014.html   1.Fiddler下载地址:https://www.tele ...

  6. linux系统层面调优

    linux系统层面调优和常见的面试题 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1664287

  7. Excel导出中HttpServletResponse消息头参数设置

    response.setCharacterEncoding("UTF-8"); //编码格式为UTF-8 response.setContentType("applica ...

  8. 004_C++常见错误类型总结(一)之最后几行错误

    1.介绍 经常进行代码测试和静态代码分析的同学,应该会遇到这样的一个问题,就是一个程序段的最后几行,或者一个源文件末尾会出现错误.本文,结合专业的静态代码分析软件PSV-Studio提供错误类型代码库 ...

  9. SpringMVC听课笔记(十三:使用拦截器)

    1.定义一个拦截器: 实现 HandlerInterceptor接口 2. 配置 3.运行流程 4.也可以通过<mvc:mapping>给拦截器设定特定的拦截路径,或者<mvc:ex ...

  10. C#9.0:Records

    概述 在C#9.0下,record是一个关键字,微软官方目前暂时将它翻译为记录类型. 传统面向对象的编程的核心思想是一个对象有着唯一标识,封装着随时可变的状态.C#也是一直这样设计和工作的.但是一些时 ...