基础

什么是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. 何为Web App,何为Hybird App

    这些概念听起来很火,当下也很流行,真正理解起来却并非易事.如果让我来全面的解释Web App和Hybird App,我觉得还有些困难. 这篇文章只是我深入了解移动领域开发过程中的不断整理和总结,其中涉 ...

  2. Windows2003终端服务器超出了最大连接数的问题解决方案

    一.重启服务器.(将现有的连接都释放) 二.修改组策略 运行--gpedit.msc--计算机配置--管理模板--Windows组件--终端服务---右边“限制连接数量” 设定“已启用”   --- ...

  3. ElasticSearch 问题分析:No data nodes with HTTP-enabled available

    环境:ES-5.4.0版本,部署方式:3master node+2client node+3data node 说明:data node和client node都配置了http.enabled: fa ...

  4. mysql中的CURRENT_TIMESTAMP

    MySQL的timestamp类型可以使用CURRENT_TIMESTAMP来指定默认值,当记录增.改时,该值会自动取当前时间,如下图所示: 增加或修改记录时,该值自动变化 但是这个跟MySQL的版本 ...

  5. js小技巧--摘录1

    原文地址https://github.com/loverajoel/jstips 1.数组中插入元素 a.尾部追加 var arr = [1,2,3,4,5]; var arr2 = []; arr. ...

  6. python 获取函数调用者

    import traceback def _mode(): print "hi---------------------------" print traceback.extrac ...

  7. How can I set ccshared=-fPIC while executing ./configure?

    解决方式如下: make clean ./configure CFLAGS=-fPIC CXXFLAGS=-fPIC

  8. 最新 IntelliJ Idea 2017 激活方法(转)

    转载地址:http://www.cnblogs.com/suiyueqiannian/p/6754091.html 1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入 ...

  9. git 切换 远程仓库

    $ git remote rm origin $ git remote add origin '仓库地址.git' $ git branch --set-upstream-to=origin/mast ...

  10. [转]asp.net权限认证:HTTP基本认证(http basic)

    本文转自:http://www.cnblogs.com/lanxiaoke/p/6353955.html HTTP基本认证示意图 HTTP基本认证,即http basic认证. 客户端向服务端发送一个 ...