博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 在XML中声明切面/AOP
阅读量:7029 次
发布时间:2019-06-28

本文共 6099 字,大约阅读时间需要 20 分钟。

在Spring的AOP配置命名空间中,我们能够找到声明式切面选择。看以下:

一、声明切面

切面就是包括切入点和通知的对象,在Spring容器中将被定义为一个Bean。Schema方式的切面须要一个切面支持Bean,该支持Bean的字段和方法提供了切面的状态和行为信息,并通过配置方式来指定切入点和通知实现。

 

    切面使用<aop:aspect>标签指定,ref属性用来引用切面支持Bean。

 

    切面支持Bean“aspectSupportBean”跟普通Bean全然一样使用。切面使用“ref”属性引用它。

二、  声明切入点

    切入点在Spring中也是一个Bean。Bean定义方式能够有非常三种方式:

    1)在<aop:config>标签下使用<aop:pointcut>声明一个切入点Bean,该切入点能够被多个切面使用。对于须要共享使用的切入点最好使用该方式,该切入点使用id属性指定Bean名字。在通知定义时使用pointcut-ref属性通过该id引用切入点。expression属性指定切入点表达式:

 
2)在<aop:aspect>标签下使用<aop:pointcut>声明一个切入点Bean
,该切入点能够被多个切面使用,但一般该切入点仅仅被该切面使用,当然也能够被其它切面使用,但最好不要那样使用,该切入点使用id属性指定Bean名字,在通知定义时使用pointcut-ref属性通过该id引用切入点。expression属性指定切入点表达式:
    
3)匿名切入点Bean。
能够在声明通知时通过pointcut属性指定切入点表达式,该切入点是匿名切入点,仅仅被该通知使用:

三、   声明通知

直接来看个样例:
package cn.com.ztz.spring.service;public interface ShowService {	public void show();}
package cn.com.ztz.spring.service;public class ShowServiceImpl implements ShowService{	@Override	public void show() {  	    showBefore();		//showError();//异常測试	    showEnd();	}	public void showBefore(){		System.out.println("showBefore============");	}	public void showError(){		System.out.println("showError============");		throw new RuntimeException();	}	public void showEnd(){		System.out.println("showEnd===============");	}}
package cn.com.ztz.spring.service;public class AudienceAspect {	public void taskSeats(){		System.out.println("等候节目開始===");	}	public void applaud(){		System.out.println("鼓掌=========");	}	public void demandRefund(){		System.out.println("退钱离场======");	}}
public static void main(String[] args) {		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  	    ShowService hs = ctx.getBean("showService", ShowService.class);  	    System.out.println("======================================");	    hs.show();	    System.out.println("======================================");	}
控制台输出结果:
======================================
等候节目開始===
showBefore============
showEnd===============
鼓掌=========
======================================

四、 为通知传递參数

如今有非常多人在观看演出,我想传入一个人姓名,怎么办了。来看下:
public interface ShowService {	public void showBefore(String param);}
public class ShowServiceImpl implements ShowService{	public void showBefore(String param){		System.out.println("showBefore============");	}}
public class AudienceAspect {	public void taskSeats(String param){		System.out.println(param+",等候节目開始===");	}}
 
public static void main(String[] args) {		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  	    ShowService hs = ctx.getBean("showService", ShowService.class);  	    System.out.println("======================================");	    hs.showBefore("张三");	    System.out.println("======================================");	}
控制台输出结果:
======================================
张三,等候节目開始===
showBefore============
======================================

五、  声明围绕通知

围绕着在切入点选择的连接点处的方法所运行的通知。围绕通知很强大,能够决定目标方法是否运行,什么时候运行,运行时是否须要替换方法參数,运行完成是否须要替换返回值,可通过<aop:aspect>标签下的<aop:around >标签声明:
围绕通知第一个參数必须是org.aspectj.lang.ProceedingJoinPoint类型,在通知实现方法内部使用ProceedingJoinPoint的proceed()方法使目标方法运行。proceed 方法能够传入可选的Object[]数组,该数组的值将被作为目标方法运行时的參数。
public interface ShowService {	public void showAround(String param);}
public class ShowServiceImpl implements ShowService{	public void showAround(String param){		System.out.println("showAround============"+param);	}}
public class AudienceAspect {	public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {  	    System.out.println("around before advice===========");  	    Object retVal = pjp.proceed(new Object[] {"around"});  	    System.out.println("around after advice===========");  	    return retVal; 	}}
public static void main(String[] args) {		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  	    ShowService hs = ctx.getBean("showService", ShowService.class);  	    System.out.println("======================================");	    hs.showAround("张三");	    System.out.println("======================================");	}
控制台输出结果:
======================================
around before advice===========
showAround============around
around after advice===========
======================================

六、引入

一些编程语言。比如Ruby和Groovy。有开放类的理念。

它们可以不用直接改动对象或类的定义可以为对象或类添加新的方法。不幸的是。java不是动态的语言,一旦编译完毕了。我们非常难再为该类加入新的功能了。

自己想想我们如今不是一直在用切面吗?实际上,利用被称为引入的AOP概念。切面能够为spring bean加入新的方法。

 Spring引入同意为目标对象引入新的接口,通过在< aop:aspect>标签内使用< aop:declare-parents>标签进行引入。

<aop:declare-parents  
          types-matching="AspectJ语法类型表达式"  
          implement-interface=引入的接口"               
          default-impl="引入接口的默认实现"  
          delegate-ref="引入接口的默认实现Bean引用"/>
看下样例:我们定义了个新的接口和实现
public interface DeclareService {	public void declare();}
public class DeclareServiceImpl implements DeclareService {	@Override	public void declare() {		System.out.println("declare=====================");	}}
public static void main(String[] args) {		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  		DeclareService hs = ctx.getBean("showService", DeclareService.class);  	    System.out.println("======================================");	    hs.declare();	    System.out.println("======================================");	}
我们获得还是showService的Bean,执行下測试方法输出结果:
======================================
declare=====================
======================================

转载地址:http://ygrxl.baihongyu.com/

你可能感兴趣的文章
解码方法
查看>>
Electron入门介绍
查看>>
从egg.js重新认识node后端开发
查看>>
聊聊springboot session timeout参数设置
查看>>
微信小程序调研
查看>>
window下git多账户管理
查看>>
【327天】我爱刷题系列086(2017.12.29)
查看>>
React.js 小书 Lesson15 - 实战分析:评论功能(二)
查看>>
如何使用JSON和GSON
查看>>
weex脚手架
查看>>
js正则表达式学习
查看>>
C++ 开发 PHP 7 扩展之定义常量
查看>>
windows 命令行禁用密码策略,创建用户
查看>>
游戏小学生01-egret环境搭建
查看>>
从零开始写爬虫
查看>>
微信小程序,个人开发者创业新平台
查看>>
Chrome vim插件vimium快捷键学习
查看>>
【Redis】Redis常用命令
查看>>
node跨域方法
查看>>
JavaScript笔记——常见DOM知识
查看>>