Spring课程 Spring入门篇 5-5 advice应用(下)
2 代码演练
2.1 环绕通知(不带参数)
2.2 环绕通知(带参数)
2 代码演练
2.1 环绕通知(不带参数)
实体类:
package com.imooc.aop.schema.advice.biz;
public class AspectBiz {
public void biz(){
System.out.println("MoocAspect biz");
// throw new RuntimeException();
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd"> <bean id = "moocAspect" class = "com.imooc.aop.schema.advice.MoocAspect"></bean>
<bean id = "AspectBiz" class = "com.imooc.aop.schema.advice.biz.AspectBiz"></bean> <aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<!-- 声明切入点:从哪里开始执行 -->
<!-- 执行com.imooc.aop.schema.advice.biz包下的所有Biz结尾的java类中的所有方法时 -->
<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPointCut"/>
<aop:before method="before" pointcut-ref="moocPointCut"/>
<aop:after-returning method="afterreturning" pointcut-ref="moocPointCut"/>
<aop:after-throwing method="throwing" pointcut-ref="moocPointCut"/>
<aop:after method="after" pointcut-ref="moocPointCut"/>
<aop:around method="around" pointcut-ref="moocPointCut"/>
</aop:aspect>
</aop:config> </beans>
通知类:
package com.imooc.aop.schema.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MoocAspect {
public void before(){
System.out.println("before");
}
public void afterreturning(){
System.out.println("after returning");
}
public void throwing(){
System.out.println("throw");
}
public void after(){
System.out.println("Say Love me");
}
public Object around(ProceedingJoinPoint pjp){
Object obj = null;
try {
System.out.println("MoocAspect Around Before");
obj = pjp.proceed();
System.out.println("MoocAspect Around After");
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
}
测试类:
package com.imooc.test.aop; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.aop.schema.advice.biz.AspectBiz;
import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvice extends UnitTestBase { public TestAOPSchemaAdvice(){
super("classpath:spring-aop-schema-advice.xml");
} @Test
public void testBiz(){
AspectBiz aBiz = super.getbean("AspectBiz");
aBiz.biz();
} }
打印日志:
四月 17, 2019 9:17:34 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@409c8a10: startup date [Wed Apr 17 21:17:34 CST 2019]; root of context hierarchy
四月 17, 2019 9:17:35 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aop-schema-advice.xml]
before
MoocAspect Around Before
MoocAspect biz
MoocAspect Around After
Say Love me
after returning
四月 17, 2019 9:17:38 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@409c8a10: startup date [Wed Apr 17 21:17:34 CST 2019]; root of context hierarchy
2.2 环绕通知(带参数)
实体类:
package com.imooc.aop.schema.advice.biz;
public class AspectBiz {
public void biz(){
System.out.println("MoocAspect biz");
// throw new RuntimeException();
}
public void init(String bizName,int times){
System.out.println("AspectBiz init:"+ bizName + " "+times);
}
}
xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd"> <bean id = "moocAspect" class = "com.imooc.aop.schema.advice.MoocAspect"></bean>
<bean id = "AspectBiz" class = "com.imooc.aop.schema.advice.biz.AspectBiz"></bean> <aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<!-- 声明切入点:从哪里开始执行 -->
<!-- 执行com.imooc.aop.schema.advice.biz包下的所有Biz结尾的java类中的所有方法时 -->
<!-- <aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPointCut"/> -->
<!-- <aop:before method="before" pointcut-ref="moocPointCut"/> -->
<!-- <aop:after-returning method="afterreturning" pointcut-ref="moocPointCut"/> -->
<!-- <aop:after-throwing method="throwing" pointcut-ref="moocPointCut"/> -->
<!-- <aop:after method="after" pointcut-ref="moocPointCut"/> -->
<!-- <aop:around method="around" pointcut-ref="moocPointCut"/> -->
<aop:around method="aroundInit" pointcut="execution(* com.imooc.aop.schema.advice.biz.AspectBiz.init(String,int)) and args(bizName,times)"/>
</aop:aspect>
</aop:config> </beans>
通知类:
package com.imooc.aop.schema.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MoocAspect {
public void before(){
System.out.println("before");
}
public void afterreturning(){
System.out.println("after returning");
}
public void throwing(){
System.out.println("throw");
}
public void after(){
System.out.println("Say Love me");
}
public Object around(ProceedingJoinPoint pjp){
Object obj = null;
try {
System.out.println("MoocAspect Around Before");
obj = pjp.proceed();
System.out.println("MoocAspect Around After");
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
public Object aroundInit(ProceedingJoinPoint pjp,String bizName,int times){
System.out.println("bizName="+bizName+" times="+times);
Object obj = null;
try {
System.out.println("MoocAspect AroundInit Before");
obj = pjp.proceed();
System.out.println("MoocAspect AroundInit After");
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
}
测试类:
package com.imooc.test.aop; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.aop.schema.advice.biz.AspectBiz;
import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvice extends UnitTestBase { public TestAOPSchemaAdvice(){
super("classpath:spring-aop-schema-advice.xml");
} @Test
public void testBiz(){
AspectBiz aBiz = super.getbean("AspectBiz");
aBiz.biz();
} @Test
public void testInit(){
AspectBiz aBiz = super.getbean("AspectBiz");
aBiz.init("moocService",3);
} }
打印日志:
四月 18, 2019 6:19:42 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@45a5049a: startup date [Thu Apr 18 06:19:42 CST 2019]; root of context hierarchy
四月 18, 2019 6:19:42 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aop-schema-advice.xml]
bizName=moocService times=3
MoocAspect AroundInit Before
AspectBiz init:moocService 3
MoocAspect AroundInit After
四月 18, 2019 6:19:43 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@45a5049a: startup date [Thu Apr 18 06:19:42 CST 2019]; root of context hierarchy
Spring课程 Spring入门篇 5-5 advice应用(下)的更多相关文章
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- Spring实践系列-入门篇(一)
本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...
- Spring课程 Spring入门篇 7-3 advice扩展
课程链接: 1 解析 1.1 advice中aspect 切面传参 1.2 通知参数名称——argNames属性, 参数为 JoinPoint.ProceedingJoinPoint.JoinPoin ...
- Spring课程 Spring入门篇 7-2 Advice定义及实例
1 解析 1.1 通知:after和afterreturning的区别 1.2 @RunWith 是什么? 2 代码演练 2.1 注解方式配置通知的两种方式 2.2 异常通知 2.3 非异常通知 1 ...
- Spring课程 Spring入门篇 6-1 Spring AOP API的PointCut、advice的概念及应用
本节主要是模拟spring aop 的过程. 实现spring aop的过程 这一节老师虽然说是以后在工作中不常用这些api,实际上了解还是有好处的, 我们可以从中模拟一下spring aop的过程. ...
- Spring课程 Spring入门篇 5-4 advice应用(上)
1 解析 1.1 通知执行顺序 2 代码演练 1 解析 1.1 通知执行顺序 aop执行方式为:前置通知==>所要增强的方法==>后置通知==>最终通知 在出现异常时会进行:前置通知 ...
- Spring课程 Spring入门篇 6-2 ProxyFactoryBean及相关内容(上)
1 解析 1.1 类的方式实现各种通知需要实现的接口 1.2 创建Spring aop代理的优点及方法 1.3 代理控制切入点和通知的顺序的代码实现(具体完全实现,见代码2.1) 1.4 代理方式选择 ...
- Spring课程 Spring入门篇 6-3 ProxyFactoryBean及相关内容(下)
1 解析 1.1 使用global advisors demo 1.2 jdk代理和cglib代理的选择 1.3 如何强制使用CGLIB实现AOP? 1.4 JDK动态代理和CGLIB字节码生成的区别 ...
- Spring课程 Spring入门篇 5-2 配置切面aspect
本节主要讲了在xml中配置切面的demo 1 解析 1.1 配置切面xml 1.2 配置切面xml 1.3 问:什么是动态代理? 2 代码演练 2.1 配置切面xml 1 解析 1.1 配置切面xml ...
随机推荐
- centos7 docker 安装 zookeeper 3.4.13 单机standalone
假设一台单机主机的ip分别为: 单机主机:192.168.0.200 1. 查找zookeeper镜像: docker search zookeeper 2. 下载官方zookeeper镜像: doc ...
- css样式引入方式,及常用设置标签样式
一. 三种样式引入方式 1. 内联式-直接写在div标签中,不推荐用 <div style="color:red;font-size:20px;font-family:'Micro ...
- windows 本地搭建 apache+mysql+php环境详细讲解
1.mysql下载配置 可参考这篇文章:https://www.cnblogs.com/myIvan/p/9265645.html 2.php下载及配置 可参考这篇文章:https://www.cnb ...
- 对csrf的理解
用户浏览并登陆信任网站A 通过验证,在用户浏览器产出A的cookie 用户在没有登出A的情况下访问攻击网站B B要求访问第三方站点A,发出一个请求 根据B的请求,浏览器带着A的cookie访问A A不 ...
- python 全栈开发:python基础
python具有优美.清晰.简单,是一个优秀并广泛使用的语言.诞生于1991年2.python历史 1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器.Python这个名字,来自 ...
- bzoj 4032(A的一个最短的子串,它不是B的子串 || A的一个最短的子串,它不是B的子序列 || A的一个最短的子序列,它不是B的子串||A的一个最短的子序列,它不是B的子序列)
在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是. 一个串的“子序列”指的是它的可以不连续的一 ...
- 使用maven搭建ssm框架的javaweb项目
目前主流的javaweb项目,常会用到ssm(Spring+Spring MVC+Mybatis)框架来搭建项目的主体框架,本篇介绍搭建SSM框架的maven项目的实施流程.记之共享! 一.SSM框架 ...
- 剑指offer——面试题22:链表中倒数第k个节点
注意代码的鲁棒性! 函数: ListNode* TheLastKthNode(ListNode* pHead,int k) { ) return nullptr; ListNode* quickNod ...
- Mbps、Kbps、KBps的关系
1. 每秒钟传输的二进制位数的单位:bps.Kbps.Mbps Mbps是Million bits per second的缩写,它的意思是时间内传输数据的平均比特数. Mbps=Mbit/s即兆比特每 ...
- UML-4-初始不是需求阶段
1.初始阶段到底做什么? 确定项目外包还是自研.成本多少. 系统边界 关键的10%的用例.非功能性需求分析,业务案例创建 开发环境准备 而需求分析是在细化阶段进行的. 2.初始阶段(或细化阶段早期)应 ...