Spring--AOP(面向切面)编程
AOP
切面就像一把菜刀,将Java处理业务流程进行分割,在分割处添加特定的业务处理。主要应用于声明事务、安全和缓存。在本文中,主要介绍两种切面的实现方法--Java配置和XML配置。
Java配置
- 创建Java类
创建一个Music的Java类,用于声明切点
@Component
public class Music {
public void perform(int num){
System.out.println("music");
}
}
- 创建切面
创建Aop Java类,并声明为切面。声明为切面使用注解@Aspect,同时,切面必须是一个Bean。同时,声明一个切点,避免创建通知的时候重复使用过长的表达式。
@Component
@Aspect
public class Aop {
@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.*(..))")
public void performer(){}
@Before("performer()")
public void beforee(){
System.out.println("before");
}
@After("performer()")
public void afterr(){
System.out.println("after");
}
@AfterReturning("performer()")
public void afterReturning(){
System.out.println("afterreturning");
}
@AfterThrowing("performer()")
public void throwingg(){
System.out.println("throwing");
}
- 创建Java配置类
创建JavaConfiguration 类,创建Bean工厂,在这里需要使用@EnableAspectAutoProxy注解启动Spring切面功能
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class JavaConfiguration {
}
- 创建测试类
创建Main测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=JavaConfiguration.class)
public class Main {
@Autowired
private Music music;
@Test
public void test(){
music.perform(3);
}
}
- 为通知传递参数
希望将声明为切点方法中的参数传递到通知当中其,则需要在声明切点的使用指明args参数,在Java中使用&& 在xml中使用and,下面是创建为通知传递参数的切面:
/*@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.perform(int)) && args(number)")
public void performer(int number){}
@Before("performer(number)")
public void beforee(int number){
System.out.println("before" + number);
}
@After("performer(number)")
public void afterr(int number){
System.out.println("after" + number+2);
}
@AfterReturning("performer(number)")
public void afterReturning(int number){
System.out.println("afterreturning" + number+1);
}
@AfterThrowing("performer(number)")
public void throwingg(int number){
System.out.println("throwing" + number +3);
}
- 另外,还可以使用@Around穿件环绕通知,被声明为环绕通知的方法需要包含参数ProceedingJoinPoint,通过ProceedingJoinPoint.proceed()来区分前置 通知和后置通知,通过try...catch来获取异常通知
@Component
@Aspect
public class AopSecond {
@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.perform(..))")
public void performer(){}
@Around("performer()")
public void per(ProceedingJoinPoint jb){
try{
System.out.println("beforeer");
jb.proceed();
System.out.println("afterr");
}catch(Throwable eThrowable){
System.out.println("exception");
}
}
}
XML中配置切面
- 创建Java类
@Component
public class AopXml {
public void performA(){
System.out.println("performA");
}
public void performB(){
System.out.println("performB");
}
}
- 创建切面类
@Component
public class AopConfug {
public void beforee(){
System.out.println("before");
}
public void afterr(){
System.out.println("after");
}
public void aroundd(ProceedingJoinPoint pb) throws Throwable{
System.out.println("beforeeee");
pb.proceed();
System.out.println("afterrrr");
}
}
- 创建xml配置文件,其中需要添加
xmlns:context="http://www.springframework.org/schema/context"和http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd,来支持component-scan,同时,还需要配置<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>支持自动注入
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<context:component-scan base-package="com.tidas.spring.fourth.aopxml"/>
<!-- 无参数 -->
<aop:config>
<aop:aspect ref="aopConfug">
<aop:pointcut expression="execution(** com.tidas.spring.fourth.aopxml.AopXml.*(..))" id="aopxmll"/>
<!-- 执行AopXml中的任何方法,都会通知切面 -->
<aop:before pointcut-ref = "aopxmll" method="beforee"/>
<aop:after pointcut-ref = "aopxmll" method="afterr"/>
<aop:around pointcut-ref = "aopxmll" method="aroundd"/>
<!-- 对于环绕通知,其实在xml中声明和其它通知声明一样,没有参数,而参数还是在具体的方法中就好 -->
</aop:aspect>
</aop:config>
</beans>
- 为通知传递参数
创建带参数的通知
//有参数
/*public void beforee(int number){
System.out.println("number:" + number);
}*/
创建带参数的方法
//有参数
public void perform(int number){
System.out.println("perform2");
}
切面和切点的配置,在xml中使用and连接args参数
<aop:config>
<aop:aspect ref="aopConfug">
<aop:pointcut expression="execution(** com.tidas.spring.fourth.aopxml.AopXml.*(int)) and args(number)" id="aopxmll"/>
<aop:before pointcut-ref = "aopxmll" method="beforee"/>
</aop:aspect>
</aop:config>
通过切面为Java引入新的功能
Spring--AOP(面向切面)编程的更多相关文章
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- spring AOP面向切面编程学习笔记
一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...
- Spring AOP面向切面编程详解
前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
- Spring AOP 面向切面编程入门
什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...
- 详细解读 Spring AOP 面向切面编程(一)
又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...
- Spring Aop面向切面编程&&自动注入
1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...
随机推荐
- idea和Webstorm上使用git和github,码云
由于之前一直使用svn,现在项目使用git,顾根据网上找的学习资料,自己梳理了下,收获蛮多,这里做个记录,如果能帮助到您那是最好不过的. 1.大致步骤 使用工具:idea,github,码云 webs ...
- 2.python数据类型
1 Number(数字) 2 字符串类型(string) 字符串内置方法 # string.capitalize() 把字符串的第一个字符大写 # string.center(width) 返 ...
- 【python】递归(阶乘、斐波纳契、汉诺塔)
- JDK源码阅读(1)_简介+ java.io
1.简介 针对这一个版块,主要做一个java8的源码阅读笔记.会对一些在javaWeb中应用比较广泛的java包进行精读,附上注释.对于容易混淆的知识点给出相应的对比分析. 精读的源码顺序主要如下: ...
- Python函数篇(6)-常用模块及简单的案列
1.模块 函数的优点之一,就是可以使用函数将代码块与主程序分离,通过给函数指定一个描述性的名称,并将函数存储在被称为模块的独立文件中,再将模块导入主程序中,通过import语句允许在当前运行的程序 ...
- 78、excel的读写操作
本篇主要是用python来自动生成excel数据文件也就是简单的excel读写操作.python读写excel文件主要是第三方模块库xlrd.xlwt. 本篇导航: 写excel 读excel 一.写 ...
- KVM(二):KVM应用
++++++++++++++++++++++++++++++创建和拍摄快照++++++++++++++++++++++++++++++++++ KVM快照方法常用的是qemu-img snapshot ...
- 关于VS2017安装的一点扩充说明(15.5)
其实逆天不推荐自己慢慢离线,找个离线包更新下再打包更快 Key:http://www.cnblogs.com/dunitian/p/4667038.html VS完整卸载工具:https://gith ...
- thinkphp3.2.3使用ajax 的一些坑——使用AjaxReturn()后,直接返回null,模板文件不起作用
从接触thinkphp到今天,填完此坑,必有其他的坑有会冒出来.哎!这个填坑之路我想是没有尽头的了. 最近,需要使用ajax完成一些操作,一开始想Ajax简单啊,不过是一种提交数据的方式,不过是害苦了 ...
- c# 去除字符串中重复字符
String.Join 和 Distinct 方法 https://www.cnblogs.com/louby/p/6224960.html 1.在写程序中经常操作字符串,需要去重,以前我的用方式利用 ...