1、关于配置文件

首先在因为要使用到扫描功能,所以xml的头文件中除了引入bean和aop之外,还要引入context才行:
<?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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> ... </beans>
x
11
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
       xmlns:context="http://www.springframework.org/schema/context"
5
       xmlns:aop="http://www.springframework.org/schema/aop"
6
       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
       http://www.springframework.org/schema/beans/spring-beans.xsd
8
       http://www.springframework.org/schema/aop
9
       http://www.springframework.org/schema/aop/spring-aop.xsd
10
       http://www.springframework.org/schema/context
11
       http://www.springframework.org/schema/context/spring-context.xsd">
12
    
13
    ...
14
    
15
</beans>    

既然使用注解,那么在配置文件中需要开启扫描配置以注册bean组件;同时Spring中使用了aspectj包的@Aspect注解标注当前组件为切面,所以同时还需要在配置文件中配置实用aspectj的自动代理模式。如下:
<!-- 开启bean组件扫描 -->
<context:component-scan base-package="dulk.learn"></context:component-scan>
<!-- 启用自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
1
 
1
<!-- 开启bean组件扫描 -->
2
<context:component-scan base-package="dulk.learn"></context:component-scan>
3
<!-- 启用自动代理 -->
4
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

2、AOP的注解配置

AOP的注解配置方式,对于一个类来说:
  • 通过 @Component 声明该类为bean组件
  • 通过 @Aspect 标记该类为切面
  • 通过注解说明类中函数的通知类型

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Component
@Aspect
public class Section { @Before("execution(* dulk.learn..*.*(..))")
public void doBefore(JoinPoint point) {
System.out.println("doBefore");
} @AfterReturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret")
public void doAfterReturning(JoinPoint point, Object ret) {
System.out.println("doAfterReturning");
System.out.println("The returning obj is " + ret);
} @AfterThrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")
public void doThrow(JoinPoint point, Throwable e) {
System.out.println("doThrow");
} @After("execution(* dulk.learn..*.*(..))")
public void doAfter() {
System.out.println("doAfter");
} @Around("execution(* dulk.learn..*.*(..))")
public void doAround(ProceedingJoinPoint point) {
System.out.println("doAround-dobefore");
try {
Object obj = point.proceed();
System.out.println("doAround-doAfterReturning");
} catch (Throwable throwable) {
System.out.println("doAround-doThrow");
}
System.out.println("doAround-doAfter");
} }
x
1
49
 
1
import org.aspectj.lang.JoinPoint;
2
import org.aspectj.lang.ProceedingJoinPoint;
3
import org.aspectj.lang.annotation.After;
4
import org.aspectj.lang.annotation.AfterReturning;
5
import org.aspectj.lang.annotation.AfterThrowing;
6
import org.aspectj.lang.annotation.Around;
7
import org.aspectj.lang.annotation.Aspect;
8
import org.aspectj.lang.annotation.Before;
9
import org.springframework.stereotype.Component;
10

11

12
@Component
13
@Aspect
14
public class Section {
15

16
    @Before("execution(* dulk.learn..*.*(..))")
17
    public void doBefore(JoinPoint point) {
18
        System.out.println("doBefore");
19
    }
20

21
    @AfterReturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret")
22
    public void doAfterReturning(JoinPoint point, Object ret) {
23
        System.out.println("doAfterReturning");
24
        System.out.println("The returning obj is " + ret);
25
    }
26

27
    @AfterThrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")
28
    public void doThrow(JoinPoint point, Throwable e) {
29
        System.out.println("doThrow");
30
    }
31

32
    @After("execution(* dulk.learn..*.*(..))")
33
    public void doAfter() {
34
        System.out.println("doAfter");
35
    }
36

37
    @Around("execution(* dulk.learn..*.*(..))")
38
    public void doAround(ProceedingJoinPoint point) {
39
        System.out.println("doAround-dobefore");
40
        try {
41
            Object obj = point.proceed();
42
            System.out.println("doAround-doAfterReturning");
43
        } catch (Throwable throwable) {
44
            System.out.println("doAround-doThrow");
45
        }
46
        System.out.println("doAround-doAfter");
47
    }
48

49
}


[10] AOP的注解配置的更多相关文章

  1. Spring之AOP的注解配置

    配置过程可以简单的分为3步: 1,业务类配置 在业务类前加入,将业务类交由Spring管理 @Component("s") 这个表示,这个业务类的Bean名字为 s . 2,将切点 ...

  2. spring的aop的注解配置

    一.使用注解的方式配置后置通知 第一步,创建通知类LogAdvice 第二步,要在spring主配置文件中声明以注解的方式配置spring aop  第三步,测试 二.其他异常配置 package c ...

  3. Spring 使用AOP——基于注解配置

    首先,使用注解实现AOP是基于AspectJ方式的. 创建包含切点方法的类 package cn.ganlixin.test; import org.aspectj.lang.annotation.P ...

  4. Spring的AOP机制---- AOP的注解配置---- AOP的注解配置

    3333隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢 ...

  5. 【Spring五】AOP之使用注解配置

    AOP使用注解配置流程: 1.当spring容器启动时候.    < context:component- scan base-package= "cn.itheima03.sprin ...

  6. Spring AOP的注解方式实现

    spring也支持注解方式实现AOP,相对于配置文件方式,注解配置更加的轻量级,配置.修改更加方便. 1.开启AOP的注解配置方式 <!-- 开启aop属性注解 --> <aop:a ...

  7. Spring(十五):通过注解配置 Bean

    在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...

  8. Spring AOP及事务配置三种模式详解

    Spring AOP简述 Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强. 使用场景如:日志打印.权限.事务控制等. 默认情况下,Spring会根据被代理的 ...

  9. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

随机推荐

  1. ionic 一些常见问题和命令

    最近项目需要用到ionic就马上去撸,但是做下来发现官方文档的native插件,按照文档来做也遇到很多坑或者暂时想不出办法实现的. ionic这种属于跨平台的开发,是适用于比较常见通用的平台,安卓机, ...

  2. [VUE ERROR] Duplicate keys detected: 'tab-user'. This may cause an update error.

    错误消息如图: 如果你看到此错误消息,则说明 v-for 指令的 key值 重复了,只需修改你的 key值 让其不会重复即可.

  3. ArcGIS Server生成文件的权限问题处理

          [arcgisserver@gisserver3 usr]$ vi /arcgis/server/usr/init_user_param.sh          

  4. 自定义合并列:el-table

    objectSpanMethod({ row, column, rowIndex, columnIndex }) {//合并规则 //当前行row.当前列column.当前行号rowIndex.当前列 ...

  5. python第十八天

    学习内容: json 模块,pickle模块,shelve模块,xml模块 json 模块  序列化: import json,pickle info={ 'name':'a', 'age':34, ...

  6. Linux进程描述符task_struct结构体详解--Linux进程的管理与调度(一)【转】

    Linux内核通过一个被称为进程描述符的task_struct结构体来管理进程,这个结构体包含了一个进程所需的所有信息.它定义在include/linux/sched.h文件中. 谈到task_str ...

  7. apache配置CA证书通过https通信

    Apache Httpd 2.2 实现https加密通讯 实际生产中CA证书一般是向一些专业认证的国际机构来进行申请的.我们会模拟使用OpenSSL生成的证书,来实现Apache的安全加密通讯,这与实 ...

  8. PHP LAMP环境搭建及网站配置流程(完整版)

    心血来潮想做一个自己的博客网站,写一些文章做技术分享,平时遇到的一些问题的解决办法都记录下来,网站搭建成功,那么第一篇博客自然就是整个网站的搭建以及域名的注册.备案.解析流程,总共分为以下几步: 1. ...

  9. January 21st, 2018 Week 3rd Sunday

    Your attitude, not your aptitude, will determine your altitude. 决定你人生高度的,不是你的才能,而是你的态度. Actually, at ...

  10. Spring boot+ maven + thymeleaf + HTML 实现简单的web项目

    第一步: 创建一个SpringBoot应用 第二步: 创建一个实体,用来存储数据,在src/main/java/com/example/first下创建包entity , 在entity下创建Pers ...