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. Linux进程管理 (2)CFS调度器

    关键词: 目录: Linux进程管理 (1)进程的诞生 Linux进程管理 (2)CFS调度器 Linux进程管理 (3)SMP负载均衡 Linux进程管理 (4)HMP调度器 Linux进程管理 ( ...

  2. keystone系列三:网关协议

    一 静态页面和动态页面 在了解了http协议后,我们知晓,一个web server的本质就是 浏览器发送一个HTTP请求: 服务器收到请求,生成一个HTML文档: 服务器把HTML文档作为HTTP响应 ...

  3. Vue+element-ui 重置组件样式的写法

    两种方式实现element-ui组件的样式 方案1:重置的公共组件样式的写法如下 然后在main.js中引入 import '@/assets/css/element.css'   方案2:每个.vu ...

  4. 【C# 复习总结】类、继承和接口

    1 类 定义新的数据类型以及这些新的数据类型进行相互操作的方法 定义方式: class Cat { } class Cat:object { } C#中所有的类都是默认由object类派生来的,显示指 ...

  5. .Net Core 在 Linux-Centos上的部署实战教程(一)

    pa我是在VS2017上写好项目然后来部署的,我的宗旨能截图就少BB 服务器系统: Asp.Net Core版本: 1.往服务器安装.net core 2.1 https://www.microsof ...

  6. Java 控制语句:循环、条件判断

    基础很重要,基础很重要,基础很重要.重要的事情说三遍,. 程序设计中的控制语句主要有三种:顺序.分支和循环.我们每天写的代码,除了业务相关,里面会包含大量的控制语句.但是控制语句的基本使用,是否有些坑 ...

  7. HDU - 4027 线段树减枝

    这题太坑了...满满的都是坑点 1号坑点:给定左右区间有可能是反的...因为题目上说x,y之间,但是没有说明x,y的大小关系(害我一直RE到怀疑人生) 2号坑点:开根号的和不等于和开根号(还好避开了) ...

  8. snappy

    Snappy 是一个 C++ 的用来压缩和解压缩的开发包.其目标不是最大限度压缩或者兼容其他压缩格式,而是旨在提供高速压缩速度和合理的压缩率.Snappy 比 zlib 更快,但文件相对要大 % 到 ...

  9. CodeIgniter框架中尝试使用swoole

    ci框架版本:3.1.7.     swoole版本:1.7.      php版本:5.6 相关文档: 以cli方式运行ci框架 swoole官方手册 创建一个TestSwoole和Hello控制器 ...

  10. Jenkins ChangeLog

    Log changes in Jenkins - Stack Overflowhttps://stackoverflow.com/questions/13631145/log-changes-in-j ...