学习 Spring (十三) AOP 配置
Spring入门篇 学习笔记
Spring 所有的切面和通知器都必须放在一个 aop:config 内(可以配置包含多个 aop:config 元素),每一个 aop:config 可以包含 pointcut, advisor 和 aspect 元素(它们必须按照这个顺序进行声明)
aop:config 风格的配置大量使用了 Spring 的自动代理机制
配置 Aspect
新建切面类:
public class MoocAspect {
}
添加配置:
<?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.karonda.aop.schema.advice.MoocAspect"></bean>
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
</aop:aspect>
</aop:config>
</beans>
配置 Pointcut
pointcut 类型说明详见:pointcut expressions
新建类:
public class AspectBiz {
}
修改配置文件:
<?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.karonda.aop.schema.advice.MoocAspect"></bean>
<bean id="aspectBiz" class="com.karonda.aop.schema.advice.biz.AspectBiz"></bean>
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut id="moocPointcut" expression="execution(* com.karonda.aop.schema.advice.biz.*Biz.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
Advice
添加依赖包 aspectjweaver:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
修改 MoocAspect:
public class MoocAspect {
public void before(){
System.out.println("MoocAspect before.");
}
public void afterReturning(){
System.out.println("MoocAspect afterReturning.");
}
public void afterThrowing(){
System.out.println("MoocAspect afterThrowing.");
}
public void after(){
System.out.println("MoocAspect after.");
}
// 环绕通知方法的第一个参数必须是 ProceedingJoinPoint 类型
public Object around(ProceedingJoinPoint pjp){
Object obj = null;
try{
System.out.println("MoocAspect around 1.");
obj = pjp.proceed();
System.out.println("MoocAspect around 2.");
}catch (Throwable e){
e.printStackTrace();
}
return obj;
}
public Object aroundInit(ProceedingJoinPoint pjp, String bizName, int times){
System.out.println(bizName + " " + times);
Object obj = null;
try{
System.out.println("MoocAspect aroundInit 1.");
obj = pjp.proceed();
System.out.println("MoocAspect aroundInit 2.");
}catch (Throwable e){
e.printStackTrace();
}
return obj;
}
}
修改 AspectBiz:
public class AspectBiz {
public void biz() {
System.out.println("AspectBiz biz.");
// throw new RuntimeException();
}
public void init(String bizName, int times){
System.out.println("AspectBiz init: " + bizName + " " + times);
}
}
修改配置:
<?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.karonda.aop.schema.advice.MoocAspect"></bean>
<bean id="aspectBiz" class="com.karonda.aop.schema.advice.biz.AspectBiz"></bean>
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut expression="execution(* com.karonda.aop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
<!--前置通知-->
<aop:before method="before" pointcut-ref="moocPiontcut"/>
<!--返回后通知-->
<aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/>
<!--抛出异常通知-->
<aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut" />
<!--后通知-->
<aop:after method="after" pointcut-ref="moocPiontcut"/>
<!--环绕通知-->
<aop:around method="around" pointcut-ref="moocPiontcut"/>
<!--带参数-->
<aop:around method="aroundInit" pointcut="execution(* com.karonda.aop.schema.advice.biz.AspectBiz.init(String, int))
and args(bizName, times)"/>
</aop:aspect>
</aop:config>
</beans>
添加测试类:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvice extends UnitTestBase {
public TestAOPSchemaAdvice(){
super("classpath:spring-aop-schema-advice.xml");
}
@Test
public void testBiz(){
AspectBiz biz = super.getBean("aspectBiz");
biz.biz();
}
@Test
public void testInit(){
AspectBiz biz = super.getBean("aspectBiz");
biz.init("moocService", 3);
}
}
源码:learning-spring
学习 Spring (十三) AOP 配置的更多相关文章
- 跟着刚哥学习Spring框架--AOP(五)
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- 跟着刚哥学习Spring框架--事务配置(七)
事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...
- Spring的AOP配置
Spring的AOP配置 1.先写一个普通类: package com.spring.aop; public class Common { public void execute(String us ...
- Spring学习笔记之AOP配置篇(一)
[TOC] 1. 创建并声明一个切面 首先,创建一个类,添加@Component注解使其添加到IoC容器 然后,添加@Aspect注解,使其成为一个切面 最后,在配置文件里面,使用<aop:as ...
- spring.net AOP配置基础
在第一篇中,我们用配置代理工厂的方式实现了面向切面记日志的功能.非常便捷的实现了AOP,但当我们需要对多个切入点配置通知的时候就需要声明多个代理工厂,这样导致配置文件内容过多,配置过程也很繁琐.spr ...
- 重新学习Spring注解——AOP
面向切面编程——思想:在一个地方定义通用功能,但是可以通过声明的方式定义这个功能要以何种方式在何处运用,而无须修改受影响的类. 切面:横切关注点可以被模块化为特殊的类. 优点: 1.每个关注点都集中在 ...
- Spring之AOP配置
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Spring框架学习-Spring的AOP概念详解
一.SpringAOP的概述. AOP(Aspect Oriented Programming),面向切面编程,通过预编译方式和运行期间动态代理实现程序的功能的统一维护的技术.AOP是OOP(面向对象 ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
随机推荐
- Scripts may close only the windows that were opened by it
关闭当前窗体报以下js错误: Scripts may close only the windows that were opened by it (脚本只能关闭由它打开的窗口) 使用场景,在js中关闭 ...
- Java关键字(五)——this
this 也是Java中的一个关键字,在<Java编程思想>第四版第五章5.4小节对 this 关键字是这样介绍的: this 关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用 ...
- jumpserver安装
一. 准备 Python3 和 Python 虚拟环境 1.1 安装依赖包 yum -y install wget sqlite-devel xz gcc automake zlib-devel o ...
- 开发手记:JedisConnectionException: Could not get a resource from the pool
对于Redis,生产环境是集群模式,测试环境是单例模式,如果在生产环境中用单例模式会报错. 解决办法,通过云配置,将配置进行自动化配置. 另附一份Redis配置: #***************** ...
- [SDOI2017]天才黑客[最短路、前缀优化建图]
题意 一个 \(n\) 点 \(m\) 边的有向图,还有一棵 \(k\) 个节点的 trie ,每条边上有一个字符串,可以用 trie 的根到某个节点的路径来表示.每经过一条边,当前携带的字符串就会变 ...
- C#调用迅雷下载,调用迅雷影音播放
方法很多种,这里介绍一种,通过命令行参数调用. try { ]; Process.Start(thunderPath, "http://www.baidu.com/abc.exe" ...
- 我去年码了个表(WPF MvvM)
又快个把月没写博客了(最近忙着学JAVA去了,都是被逼的/(ㄒoㄒ)/~~),然后用WPF码了个表,其实想加上那种提醒功能什么的,额,就这样了吧,主要是感受一下数据驱动的思想. 效果如下: 前端XAM ...
- Jenkins-job之间依赖关系配置
使用场景: 想要在某APP打新包之后,立即执行自动化测试的job来验证该新包. 比如Job A 执行完执行Job B ,如下图所示,如何建立依赖呢? 1.配置上游依赖 构建触发器-配置如下信息: 选择 ...
- [LeetCode] Rank Scores -- 数据库知识(mysql)
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- WinRAR从入门到高级的操作技巧集合
一.基础技巧 1.批量建立文件夹 如果在工作中,经常要建立很多相同文件夹结构(如在备份数据时).那可以把这个繁琐的工作让WinRAR完成:先在“资源管理器”中把多个文件夹结构建好(包括其下的子文件夹) ...