一、简述

前面讲了基于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. rawbytestring

    rawbytestring Delphi 定义了 RawByteStrng 类型的字符串,定义如下: RawByteString = type AnsiString($ffff); 关于RawByte ...

  2. 详解Java Spring各种依赖注入注解的区别

    注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Comp ...

  3. java基础之static(静态)

    静态的属性.方法等属于类而不是对象. 静态的方法能够由类直接调用,不须要将类实例化. 本篇主要说明:1.态的代码.成员变量要比构造方法先运行. 2. 子类的构造方法会默认去调用父类的不带參数的构造方法 ...

  4. jquery 实现table的行列选中效果改进

    行列都可以多选,也可对相应数据进行统计: 行选中效果 列选中效果

  5. Linux 安全信息查看

    终端登录情况 last ssh登录情况 cat /var/log/secure | grep -i "accepted password" 定时任务 cat /var/log/cr ...

  6. 代码实现Android5.0的下拉刷新效果

    如图所示,实现类似与gmail的下拉刷新. 项目地址:https://github.com/stormzhang/SwipeRefreshLayoutDemo 一.在xml文件中定义 这个控件在sup ...

  7. [转]hive metadata 存mysql 注释中文乱码的有关

    FROM : http://blog.csdn.net/tswisdom/article/details/41444287 hive metadata 存mysql 注释中文乱码的问题 hive me ...

  8. [转]MySQL单列索引和组合索引的区别介绍

    FROM : http://database.ctocio.com.cn/353/11664853.shtml MySQL单列索引是我们使用MySQL数据库中经常会见到的,MySQL单列索引和组合索引 ...

  9. asp.net 判断用户是否使用微信浏览器

    平时我们看一些网页的时候会发现这样的功能:有的页面只能在微信里访问,如果在电脑上访问就提示用户请到微信上访问该网页.这个用C#怎么实现呢?我们结合代码来看看. 首先,我们需要先判断用户使用的是什么浏览 ...

  10. CentOS下httpd下php 连接mysql 本机可以,外网报错Could not connect: Can't connect to MySQL server on '127.0.0.1' (13)2003 原因解析

    php代码很简单: $server="127.0.0.1"; println("Begin"); $link = mysql_connect($server,& ...