一、简述

前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现。

基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入。

<?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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/>
/>

当然,这一步也可以通过注解来实现,来看代码吧。

二、步骤

  1、引入依赖

    和前面讲的一样,先引入Spring-Aop和AspectJ的依赖

  

        <!--测试1使用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--测试2、3、4、5、6使用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--测试Aop使用-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>

  2、在/src/test/java/aoptest2下建立下列类:

package aoptest2;

import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component; @Component
public class MyTeacher {
public void aopPointMethod1() {
System.out.println("this is aopPointMethod1 executed.");
}
}
package aoptest2;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; @Aspect
@Component@EnableAspectJAutoProxy //需要启用注解方式的AOP
public class MyTeacherExtension {
@Before("execution(* aoptest2.MyTeacher.aopPointMethod1(..))")
public void aopInspectAtBefore() {
System.out.println("this is aopInspectAtBefore method execute.");
} @AfterReturning("execution(* aoptest2.MyTeacher.aopPointMethod1(..))")public void aopInspectAtAfterReturing() {
System.out.println("this is aopInspectAtAfterReturing method execute.");
}
@After("execution(* aoptest2.MyTeacher.aopPointMethod1(..))")public void aopInspectAtAfter() {
System.out.println("this is aopInspectAtAfter method execute.");
}
@Around("execution(* aoptest2.MyTeacher.aopPointMethod1(..))")public void aopAround(ProceedingJoinPoint proceedingJoinPoint) {
try {
System.out.println("aopAround1");
Object obj = proceedingJoinPoint.proceed();
System.out.println("aopAround2");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
} }

  3、添加一个测试类:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AopTest {
@Test
public void aopTest2() {
ApplicationContext context = new AnnotationConfigApplicationContext("aoptest2");
aoptest2.MyTeacher mywoker = context.getBean(aoptest2.MyTeacher.class);
mywoker.aopPointMethod1();
}
}

  4、运行测试

aopAround1
this is aopInspectAtBefore method execute.
this is aopPointMethod1 executed.
aopAround2
this is aopInspectAtAfter method execute.
this is aopInspectAtAfterReturing method execute.

使用Spring框架入门四:基于注解的方式的AOP的使用的更多相关文章

  1. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  2. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  3. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Spring的事务控制-基于注解的方式

    模拟转账操作,即Jone减少500,tom增加500 如果有疑问请访问spring事务控制-基于xml方式 1.创建数据表 2.创建Account实体类 public class Account { ...

  5. Spring框架学习07——基于传统代理类的AOP实现

    在Spring中默认使用JDK动态代理实现AOP编程,使用org.springframework.aop.framework.ProxyFactoryBean创建代理是Spring AOP 实现的最基 ...

  6. Spring Boot入门(四):开发Web Api接口常用注解总结

    本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...

  7. 【原创】NIO框架入门(四):Android与MINA2、Netty4的跨平台UDP双向通信实战

    概述 本文演示的是一个Android客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo. 当前由于NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能. ...

  8. Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis

    https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...

  9. Mybatis框架基于注解的方式,实对数据现增删改查

    编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...

随机推荐

  1. java共享锁实现原理及CountDownLatch解析

    前言 前面介绍了ReentrantLock,又叫排他锁,本篇主要通过CountDownLatch的学习来了解java并发包中是如何实现共享锁的. CountDownLatch使用解说 CountDow ...

  2. 升级IOS8游戏上传自定义头像功能失效的问题

    为了支持arm64,之前已经折腾了很久,昨晚打包准备提交苹果审核时,测试那边的同事反馈说游戏上传自定义头像功能不可用了. 游戏上传自定义功能的简介:卡牌游戏最初是<比武招亲>中有一个充VI ...

  3. 【转】Android开发在路上:少去踩坑,多走捷径

    本文是我订阅"腾讯大讲堂"公众帐号时,他们推送的一篇文章,但在腾讯大讲堂官网上我并没有找到这篇文章,不过其它专门"爬"公众号文章的网站倒是有.我觉得写的很不错. ...

  4. ios成长之每日一遍(day 8)

    这几天都有一些任务要跟, 把ios的学习拉后, 看看要抓紧咯, 看看轮到的学习的是UITableView. BIDAppDelegate.h #import <UIKit/UIKit.h> ...

  5. Fragment Transactions & Activity State Loss

    转自:http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html The foll ...

  6. ExtJS 4.2 教程-02:bootstrap.js 工作方式

    转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-2-bootstrap-js ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4.2 ...

  7. centos7 开机启动管理

    查看开机启动项 ls /etc/systemd/system/multi-user.target.wants/ 设置开机启动 systemctl enable mongodb.service

  8. 直方图与bin

    1.bin的含义 直方图中bin的含义:计算颜色直方图需要将颜色空间划分为若干小的颜色区间,即直方图的bin,通过计算颜色在每个小区间内德像素得到颜色直方图,bin越多,直方图对颜色的分辨率越强,但增 ...

  9. 利用Linux的Samba服务模拟NT域

    利用Linux的Samba服务模拟NT域 Samba是一个与Windows NT具有相同协议的软件包.我们可以利用Samba服务来模拟 Windows NT域,使用户从Windows计算机上直接使用一 ...

  10. 使用Idea创建多Module工程

    1. 点击 New -- Project 2. 设置工程父Pom, 如下 <?xml version="1.0" encoding="UTF-8"?> ...