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进程管理总结

    目录 一.进程相关的概念 二.关闭会话时子进程进程被杀死 三.nohup的原理 四.setsid原理 五.daemon &和守护进程的区别 六.服务进程为什么要fork两次 七.systemd ...

  2. Volley使用

    Volley是常用的网络请求框架,主要的用法如下: 获取字符串: public static void volleyTest1(final Context context){ RequestQueue ...

  3. Java多线程核心技术(五)单例模式与多线程

    本文只需要考虑一件事:如何使单例模式遇到多线程是安全的.正确的 1.立即加载 / "饿汉模式" 什么是立即加载?立即加载就是使用类的时候已经将对象创建完毕,常见的实现办法就是直接 ...

  4. 原生JS实现三级联动

    代码实现 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  5. make太慢了,加快编译速度的方法 make -j

    make -j 既然IO不是瓶颈,那CPU就应该是一个影响编译速度的重要因素了. 用make -j带一个参数,可以把项目在进行并行编译,比如在一台双核的机器上,完全可以用make -j4,让make最 ...

  6. windows开机启动bat文件

    1.运行 shell:startup 命令,如下: 2.创建bat的快捷方式,把改快捷方式添加到,C:\ProgramData\Microsoft\Windows\Start Menu\Program ...

  7. win8.1系统下安装ubuntu实现双系统实践教程

    寒假闲来无事,一程序猿哥们给发了一个linux的shell编程指南,看了几张感觉不错.于是装一个试试. 没想到一装才知道了那么的问题. 下面开始: step 1: 软件准备:Ubuntu 系统镜像,这 ...

  8. Linux—vim常用命令

    vim常用命令: 1. 键入i进入编辑模式2. esc进入命令模式3. a,进入编辑模式3. b,光标移动到单词前,end,光标移动到行尾4. home光标移动到行首5. cc,删除当前行,并进入编辑 ...

  9. 543A - Writing Code(二维动态规划)

    题意:现在要写m行代码,总共有n个文件,现在给出第i个文件每行会出现v[i]个bug,问你在bug少于b的条件下有多少种安排 分析:定义dp[i][j][k],i个文件,用了j行代码,有k个bug 状 ...

  10. ElasticSearch 入门

    http://www.oschina.net/translate/elasticsearch-getting-started?cmp ElasticSearch 简单入门 返回原文英文原文:Getti ...