springAOP学习笔记
基础
什么是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学习笔记的更多相关文章
- [原创]java WEB学习笔记104:Spring学习---AOP 前奏,通过一个问题引入动态代理
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Mybatis学习笔记二
本篇内容,紧接上一篇内容Mybatis学习笔记一 输入映射和输出映射 传递简单类型和pojo类型上篇已介绍过,下面介绍一下包装类型. 传递pojo包装对象 开发中通过可以使用pojo传递查询条件.查询 ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
- 23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy
23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy 2016-07-18 (www.cnblogs.com/icmzn) 模式理解
- mybatis 学习笔记(四):mybatis 和 spring 的整合
mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...
- Spring学习笔记(六)—— SSH整合
一.整合原理 二.整合步骤 2.1 导包 [hibernate] hibernate/lib/required hibernate/lib/jpa 数据库驱动 [struts2] struts-bla ...
- Spring实战第四章学习笔记————面向切面的Spring
Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...
- Spring实战第一章学习笔记
Spring实战第一章学习笔记 Java开发的简化 为了降低Java开发的复杂性,Spring采取了以下四种策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...
- java maven、springmvc、mybatis 搭建简单Web项目学习笔记
前言: 空余的时间,学学 Java,没准哪天用的到: 环境搭建折腾了好几天,总算搞顺了,也做个学习笔记,以防后面会忘记: 一.安装文件及介绍 JDK:jdk1.8.0 77 eclipse-maven ...
随机推荐
- try -catch-finally一些要点
try -catch-finally是处理程序异常时使用,当程序正常时,先走try然后到finally语句,不正常时:程序先走try,然后到catch里面的语句,最后到finally;从上面可以看出, ...
- Ubuntu 下 unzip用法
unzip [参数] <压缩文件> 参数: -P <密码> zip 压缩包的密码-f 覆盖原有文件-d <路径> 指定解压路径-n 解压缩时不覆盖原有文件-o 不经 ...
- CentOS 开启安装EPEL YUM源
我们用yum安装软件时,经常发现我们的yum源里面没有该软件,需要自己去wget,然后configure,make,make install,太折腾了. 其实,CentOS 还有一个源叫做 EPEL ...
- GitHub注册和Git安装
一.注册GitHub GitHub官方地址:https://github.com. 在浏览器中打开GitHub网址,通过首页进行注册,如下图所示. 二.安装Git Git官方下载地址:http://g ...
- https证书pfx 生成 pem,crt,key
(1)将.pfx格式的证书转换为.pem文件格式: openssl pkcs12 -in xxx.pfx -nodes -out server.pem (2)从.pem文件中导出私钥server ...
- archlinux安装gnome的一些坑随记
问题1:网络设置无法查看,提示缺少NetworkManager 解决:安装networkmanager库,因为gnome调用的是networkmanager这个软件来管理网络的.然后要启动它:sudo ...
- leetcode2:线性表
/********************************************** Function:input two array and find the kth value the ...
- 第一章:初识PHP
Web程序工作原理介绍? 工作方式? 浏览器首先向Web服务器发出请求,Web服务器对请求做出响应,将响应数据发送给客户端浏览器,通常是一个HTML文件,然后通过浏览器把HTML文件信息显示在屏幕上. ...
- php 截取中文字符串方法
/** * 截取中文字符串函数 * @param $str 需要截取的字串 * @param $start 开始截取的位置 * @param $length 截取的长度 * @return 此函数返回 ...
- iOS开发之工具篇-20个可以帮你简化移动app开发流程的工具
如果想进入移动app开发这个领域,你总能从别的开发者或者网上或者书上找到各种各样的方法和工具,对于新手来说,还没有摸清门路就已经陷入迷茫了.这里推荐20个可以帮你简化app开发流程的工具.很多开发者都 ...