一、简述

前面讲了基于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. iOS 使用宏 常量 报错 expected expression

    报错的代码: 报错原因:多写了一个分号!

  2. 解决克隆 centos虚拟机后修改克隆后的机器的ip、mac、uuid失败的问题

    解决办法:     So here's how we fix it: Remove the kernel's networking interface rules file so that it ca ...

  3. WordPress基础:get_page_link获取页面地址

    函数:get_page_link(页面id编号) 作用:获取指定页面的链接地址 用法: $link = get_page_link(2); 输出为:xxx/?page_id=2 如在循环里则不用填写i ...

  4. 架构:The Onion Architecture : part 3(洋葱架构:第三篇)(转载)

    In my previous installments, I described what has become my approach to defining the architecture fo ...

  5. OCP-1Z0-051-题目解析-第22题

    22. You need to create a table for a banking application. One of the columns in the table has the fo ...

  6. 来自Google的TCP BBR拥塞控制算法解析

    转自:http://blog.csdn.net/dog250/article/details/52830576 写本文的初衷一部分来自于工作,更多的来自于发现国内几乎还没有中文版的关于TCP bbr算 ...

  7. win7凭据管理、win7多用户远程登录、主机头设置、nuget.org无法访问

    前言  最近遇到的几个问题,然后处理在此对处理方式进行记录一下. 1.服务器共享文件夹,在本机进行访问登录时,每次登录或者每次开机进入都要进行登录的权限认证,这样很麻烦. 2.服务器难免会有多用户同时 ...

  8. bat与jscript开发工具时遇到的一些问题

    之前使得bat调用luac进行编译时,会弹出一个"黑色的界面",闪烁一下,感觉不太好.而脚本vbs或者jscript调用bat是可以利用Run方法,将其第二个参数设置为0便可以隐藏 ...

  9. [转]Haproxy 1.5.0 正式发布,Web 负载均衡

    From : http://www.oschina.net/news/53070/haproxy-1-5-0 经过 4 年的不懈努力,HAProxy 1.5.0 终于发布了! 相对于 1.4 版本来说 ...

  10. Chapter 6 -- Caches

    CachesExplained Explanation for how to use Guava caches. explained Updated Jun 4, 2013 by lowas...@g ...