Spring课程 Spring入门篇 5-5 advice应用(下)
2024-09-26 02:33:51
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
最新文章
- RabbitMQ学习系列(五): RPC 远程过程调用
- Ubuntu 中 不显示WIFI解决方法
- Maven编译jar出现:无法确定 T 的类型参数的异常的原因和处理方案
- 使用inherit属性值继承其父元素样式来覆盖UA自带样式。
- 远程方法调用(RMI)原理与示例
- Week3 博客阅读感想和代码复审
- Objective-C之@class的使用
- session讲解(二)——商城购物车练习
- [转载]Android开发常用调试技术记录
- Javascript中setTimeout和setInterval的区别和使用
- DM8168 环境搭建(2) ------ 虐心之旅
- 参数化时按行读取txt文件,如何去掉换行符";\n";
- Tr A
- angularjs——工具方法
- QT IP输入框正则表达式(使用QLineEdit的setValidator函数)
- 解决ASP.NET Web API Json对象循环参考错误
- javascript动画效果之匀速运动(修订版)
- WDA基础十六:ALV的颜色
- JAVA日常之一
- CVE-2017-12615和CVE-2017-12616