基础

什么是aop?

把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的

基础上,对我们的已有方法进行增强。

引用

```

org.aspectj
aspectjweaver
1.8.13

```

AOP方法

import org.aspectj.lang.ProceedingJoinPoint;

public class AopMethod {

    public void before() {
System.out.println("前置通知");
} public void afterReturning() {
System.out.println("后置通知");
} public void afterThrowing() {
System.out.println("异常通知");
} /**
* 环绕通知需要环绕通知的前置通知执行完成后,让原有的方法执行,再执行环绕通知的后置通知
*/
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕通知-前置"); //执行原来的方法
joinPoint.proceed(); System.out.println("环绕通知-后置");
} public void after() {
System.out.println("最终通知");
}
}

使用

xml配置

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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"> <bean id="ceshi" class="com.alvin.service.Ceshi"/>
<bean id="aopmethod" class="com.alvin.aop.AopMethod"/> <aop:config>
<aop:aspect ref="aopmethod">
<aop:before method="before" pointcut="execution(* com.alvin.service.*.m*(..))"/>
<aop:after-returning method="afterReturning" pointcut="execution(* com.alvin.service.*.m*(..))"/>
<aop:around method="around" pointcut="execution(* com.alvin.service.*.m*(..))"/>
<aop:after method="after" pointcut="execution(* com.alvin.service.*.m*(..))"/>
</aop:aspect>
</aop:config>
</beans>

注解配置

  • 开启注解

springmvc.xml

<!--配置开启AOP的注解使用@EnableAspectJAutoProxy-->
<aop:aspectj-autoproxy/>
  • 配置AOP
@Component
@Aspect
public class AopMethod { @Before("execution(* com.alvin.service.Ceshi.method())")
public void before() {
System.out.println("前置通知");
} }

或者配置config

@Configuration
@ComponentScan("com.alvin")
@Import(JdbcConfig.class)
@EnableAspectJAutoProxy
public class SpringConfig {
}

springAOP学习笔记的更多相关文章

  1. [原创]java WEB学习笔记104:Spring学习---AOP 前奏,通过一个问题引入动态代理

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

  2. Mybatis学习笔记二

    本篇内容,紧接上一篇内容Mybatis学习笔记一 输入映射和输出映射 传递简单类型和pojo类型上篇已介绍过,下面介绍一下包装类型. 传递pojo包装对象 开发中通过可以使用pojo传递查询条件.查询 ...

  3. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  4. 23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy

    23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy 2016-07-18 (www.cnblogs.com/icmzn) 模式理解

  5. mybatis 学习笔记(四):mybatis 和 spring 的整合

    mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...

  6. Spring学习笔记(六)—— SSH整合

    一.整合原理 二.整合步骤 2.1 导包 [hibernate] hibernate/lib/required hibernate/lib/jpa 数据库驱动 [struts2] struts-bla ...

  7. Spring实战第四章学习笔记————面向切面的Spring

    Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...

  8. Spring实战第一章学习笔记

    Spring实战第一章学习笔记 Java开发的简化 为了降低Java开发的复杂性,Spring采取了以下四种策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...

  9. java maven、springmvc、mybatis 搭建简单Web项目学习笔记

    前言: 空余的时间,学学 Java,没准哪天用的到: 环境搭建折腾了好几天,总算搞顺了,也做个学习笔记,以防后面会忘记: 一.安装文件及介绍 JDK:jdk1.8.0 77 eclipse-maven ...

随机推荐

  1. Mac 10.12安装Atom文本增强编辑工具

    下载: https://atom.io/

  2. Mac 10.12安装Office 2011

    说明:由于WPS没有Mac版的,所以只能装这个,这个版本是2011的,基本够用了. 下载: (链接: https://pan.baidu.com/s/1pKGvXBP 密码: irw9)

  3. MongoDB与c#(二)简单例子 使用1.7版本驱动

    //创建数据库链接 在1.7的版本驱动中这样写是会报 MongoServer方法已过时的            //MongoServer server =  MongoDB.Driver.Mongo ...

  4. (转)Mysql占用过高CPU时的优化手段

    Mysql占用CPU过高的时候,该从哪些方面下手进行优化?占用CPU过高,可以做如下考虑:1)一般来讲,排除高并发的因素,还是要找到导致你CPU过高的哪几条在执行的SQL,show processli ...

  5. ok6410 android driver(2)

    I will paste and anlaysis a small character device driver in this paragraph. #include <linux/modu ...

  6. cocos2d-x中描述精灵帧图片的plist和json文件各个key的含义

    最近在研究cocos,互联网行业中,手游业最近的表现是非常的火,加上本身对游戏有浓厚兴趣,所以便染指了游戏引擎~ 这次的废话就这么简短吧,因为这次记录的东西本身就很少. 在cocos中,为精灵帧添加缓 ...

  7. python笔记01-----列表操作

    在python中列表用 '[]' 表示 列表的查询操作 列表的切片 names = ["a","b","c"]             #定 ...

  8. [中英对照]How PCI Works | PCI工作原理

    How PCI Works | PCI工作原理 Your computer's components work together through a bus. Learn about the PCI ...

  9. 什么是SharePoint?

    在聊SharePoint开发之前,有必要说下什么是SharePoint. 在我工作的过程中,经常遇到客户对SharePoint不太了解的情况.有客户说,SharePoint太烂了,DropBox能做到 ...

  10. R语言变量赋值

    变量可以使用向左,向右且等于操作符来分配值.可以使用 print() 或 cat() 函数打印变量的值.cat() 函数将多个项目并成连续并打印输出. # Assignment using equal ...