基础

什么是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. Highcharts图表.net版开源,支持webform 和 mvc3,完全开源

    Highcharts是一个制作图表的纯Javascript类库,主要特性如下: 兼容性:兼容当今所有的浏览器,包括iPhone.IE和火狐等等: 对个人用户完全免费: 纯JS,无BS: 支持大部分的图 ...

  2. Hibernate中Query.list()方法报IllegalArgumentException异常

    最近在使用Hibernate开发项目,在写好hql语句,并初始化Query对象,执行Query.list()方法时,应用报IllegalArgumentException异常.经网上查询,现已经基本决 ...

  3. javascript全局方法与变量

    1.encodeURI(URI) a.作用:是对统一资源标识符(URI)进行编码的方法: b.参数:是一个完整的URI: c.特点:不需要对保留字以及在URI中有特殊意思的字符进行编码. (1).保留 ...

  4. linux mint 19 pyenv 安装 python 3.7.0 问题解决

    Python3: ImportError: No module named '_ctypes' 解决 sudo apt-get install libffi-dev WARNING: The Pyth ...

  5. PHP之string之chr()函数使用

    chr (PHP 4, PHP 5, PHP 7) chr - Return a specific character chr - 返回指定的字符 Description string chr ( i ...

  6. java值传递与引用传递

    看代码: import java.lang.reflect.Field; public class Test { public static void main(String[] args) { In ...

  7. 转换json和字符串的一些方法

    将字符串转换成json对象的方法: var str = '{"name1":"value1","name2":"value2&qu ...

  8. iOS开源项目周报0302

    由OpenDigg 出品的iOS开源项目周报第十期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.TodayMin ...

  9. 第9章 scrapy-redis分布式爬虫

    9-1 分布式爬虫要点 1.分布式的优点 充分利用多机器的宽带加速爬取 充分利用多机的IP加速爬取速度 问:为什么scrapy不支持分布式? 答:在scrapy中scheduler是运行在队列的,而队 ...

  10. RabbitMQ---7、常见参数含义

    简介 本节主要讨论队列声明的各个参数 queueDeclare(String queue, boolean durable, boolean exclusive, Map<String, Obj ...