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 ...
随机推荐
- Struts2学习---result结果集
这一章节主要介绍如何配置结果集,分为以下几个知识点: 结果集类型(result type) 全局结果集(global types) 动态结果集(dynamic type) 带有参数的结果集(type ...
- 结构体struct sockaddr_in, struct sockaddr,struct in_addr
一.结构体 struct sockaddr_in, struct sockaddr, struct in_addr struct sockaddr_in, struct sockaddr,str ...
- iOS学习之Socket使用简明教程- AsyncSocket
转载自:http://my.oschina.net/joanfen/blog/287238 如果需要在项目中像QQ微信一样做到即时通讯,必须使用socket通讯,本人也是刚学习,分享一下,有什么不对的 ...
- 如何高效撤销Git管理的文件在各种状态下的更改
一.背景 企业中我们一般采用分布式版本管理工具git来进行版本管理,在团队协作的过程中,我们难免会遇到误操作,需要撤销更改的情况,那么我们怎么高效的进行撤销修改呢?对于还未提交到暂存区的代码怎么高效撤 ...
- mac上虚拟机安装旧版本的macosx 10.8
前言 由于测试的需要,需要10.8的macosx,但又不想降级自己mac版本,所以还是装虚拟机,Parallels Desktop试验了安装不了osx,就换VMware Fusion,发现是可以的. ...
- 如何在MQ中实现支持任意延迟的消息?
什么是定时消息和延迟消息? 定时消息:Producer 将消息发送到 MQ 服务端,但并不期望这条消息立马投递,而是推迟到在当前时间点之后的某一个时间投递到 Consumer 进行消费,该消息即定时消 ...
- MySQL数据库入门(建库和建表)--陈远波
建库.建表 1.建库 (1)SQL语句命令建库: Create database数据库名称 (该方法创建的数据库没有设置编码乱码) 1 2 3 4 5 -- 创建数据库时,设置数据库的编码方式 -- ...
- lesson - 3 笔记 ls /alias /ldd /cd /pwd /环境变量 / 目录
一.ls 命令 作用:用来显示目录列表. 语法: ls (选项) (参数) 选项: -a: 显示所有档案以及目录(ls内定将档案或目录名称为“./..”的视为隐藏) -A: 显示除隐藏文件“./.. ...
- 第三节 - centos 内核启动、救援模式、 ls 、目录结构
Linux 第三节一.CentOS 启动: 1.内核引导: 1.win/linux 通电,2.BISO自检(CPU,内存,硬盘等 | U盘.光驱.网卡.硬盘启动 通过MBR知道内核内存硬件驱动位置并加 ...
- Selenide UI 自动化测试
我没有拼写错误,确实不是 Selenium ,但是,只要是 Web UI 自动化测试框架,基本上都是基于Selenium 的.Selenide 也不例外.那为啥不直接用Selenium呢? 因为 ...