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 配置的更多相关文章

  1. 跟着刚哥学习Spring框架--AOP(五)

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  2. 跟着刚哥学习Spring框架--事务配置(七)

    事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...

  3. Spring的AOP配置

    Spring的AOP配置 1.先写一个普通类: package com.spring.aop; public class Common {  public void execute(String us ...

  4. Spring学习笔记之AOP配置篇(一)

    [TOC] 1. 创建并声明一个切面 首先,创建一个类,添加@Component注解使其添加到IoC容器 然后,添加@Aspect注解,使其成为一个切面 最后,在配置文件里面,使用<aop:as ...

  5. spring.net AOP配置基础

    在第一篇中,我们用配置代理工厂的方式实现了面向切面记日志的功能.非常便捷的实现了AOP,但当我们需要对多个切入点配置通知的时候就需要声明多个代理工厂,这样导致配置文件内容过多,配置过程也很繁琐.spr ...

  6. 重新学习Spring注解——AOP

    面向切面编程——思想:在一个地方定义通用功能,但是可以通过声明的方式定义这个功能要以何种方式在何处运用,而无须修改受影响的类. 切面:横切关注点可以被模块化为特殊的类. 优点: 1.每个关注点都集中在 ...

  7. Spring之AOP配置

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  8. Spring框架学习-Spring的AOP概念详解

    一.SpringAOP的概述. AOP(Aspect Oriented Programming),面向切面编程,通过预编译方式和运行期间动态代理实现程序的功能的统一维护的技术.AOP是OOP(面向对象 ...

  9. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

随机推荐

  1. 获取数值型数组中大于60的元素个数,给数值型数组中不足60分的加20分。(数组,for循环,if条件判断语句)

    package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public c ...

  2. 开源版本PowerShell Core 6.2 发布

    导读 PowerShell Core 6.2 GA 已发布,PowerShell Core 是 PowerShell 的开源版本,适用于 Linux,macOS 和 Windows. 有关 Power ...

  3. Golang 学习资料

    资料 1.How to Write Go Code https://golang.org/doc/code.html 2.A Tour of Go https://tour.golang.org/li ...

  4. 【mongoDB查询进阶】聚合管道(二) -- 阶段操作符

    https://segmentfault.com/a/1190000010826809 什么是管道操作符(Aggregation Pipeline Operators) mongoDB有4类操作符用于 ...

  5. Netty学习问题总结

    目录 一.HTTP协议分包 二.WebSocket协议分包 三.HTTP和WebSocket协议共用一个端口的问题 四.TIME WAIT状态占用了什么资源 五.关于 本篇记录了Netty学习过程中想 ...

  6. C#发邮件_EmailHelper

    EmailHelper类 public class EmailHelper { /// <summary> /// 发送邮件 /// </summary> /// <pa ...

  7. NO NEWS IS GOOD NEWS

    从客户那传来一个噩耗:要求每个表单在保存之后,要在页面上弹一个 “ 保存成功 ” 的对话框. 客户代表志得意满地说这样用户体验更好,略带谴责意味地傲娇道,“你们早该想到的”.呵呵…… 可不是嘛,我刚入 ...

  8. 语法设计——基于LL(1)文法的预测分析表法

    实验二.语法设计--基于LL(1)文法的预测分析表法 一.实验目的 通过实验教学,加深学生对所学的关于编译的理论知识的理解,增强学生对所学知识的综合应用能力,并通过实践达到对所学的知识进行验证.通过对 ...

  9. vue2.0之axios使用详解

    axios 基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 功能特性 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 htt ...

  10. linus 下redis守护进程启动

    修改配置文件 sudo vim /usr/src/redis/redis.conf // 具体的安装目录不一样,以安装的时候为准 # 将daemonize 改为yes daemonize yes 重新 ...