Spring AOP 之 Schema配置切面
来源:互联网 发布:淘宝挂件 青花瓷 编辑:程序博客网 时间:2024/05/14 11:26
这篇文章是基于Spring AOP 之 Aspect文章的一个延伸,如果不熟悉如何使用AspectJ annotation去配置切面的话 建议先去看《Spring AOP 之 Aspect》 下面我们就使用一个比较简单的例子介绍如何使用spring配置文件去配置切面:
定义Aspect类,但是我们不标记任何AspectJ的annotation:
public class AspectExample { public void begin(JoinPoint point){ System.out.println("method name : " + point.getSignature().getName()); }}
配置文件配置如下:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 业务service --> <bean id="userService" class="com.maxfunner.service.impl.UserService" /> <bean id="adminService" class="com.maxfunner.service.impl.AdminService" /> <bean id="aspectExample" class="com.maxfunner.aspect.AspectExample" /> <aop:config proxy-target-class="true"> <aop:aspect ref="aspectExample"> <aop:before method="begin" pointcut="execution(* com.maxfunner.service..*(..))" /> </aop:aspect> </aop:config></beans>
可以发现这个跟我们使用annotation配置基本上差不多,其理念几乎相同,非常容易理解。当然我们可以额外的去定义一个pointcut标签进行引用:
<aop:config proxy-target-class="true"> <aop:pointcut id="allServiceMethod" expression="execution(* com.maxfunner.service..*(..))" /> <aop:aspect ref="aspectExample"> <aop:before method="begin" pointcut-ref="allServiceMethod" /> </aop:aspect></aop:config>
其他基本上大同小异,但是我们在这里贴出一下引介增强,这个还是比较提别的:
<aop:config proxy-target-class="true"> <aop:aspect> <aop:declare-parents types-matching="com.maxfunner.service.impl.UserService" implement-interface="com.maxfunner.service.AccountSecurity" default-impl="com.maxfunner.service.impl.AccountSecurityImpl" /> </aop:aspect></aop:config>
其调用调用方式基本上与AspectJ annotation配置方式的使用方式一致。
Advisor配置
在之前我们会使用定义一个advisor的类去继承如StaticMethodMatcherPointcutAdvice的类,进行advisor定义,但是我们可以直接使用aop:advisor去简化这一切,当然如果需要使用advisor配置,我们需要定义相应的advice进行增强操作:
public class UserAdvice implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println(">>>>>>>>>>" + method.getName() + " invoked !!!"); }}下面我们就使用Advisor配置去使用这个advice:
<bean id="userAdvice" class="com.maxfunner.advice.UserAdvice" /><aop:config proxy-target-class="true"> <aop:advisor advice-ref="userAdvice" pointcut="execution(* com.maxfunner.service.impl.UserService.login(..))" /></aop:config>
当然这些操作也是可以混合的去使用,如果你愿意的话。
0 0
- Spring AOP 之 Schema配置切面
- 【Spring AOP】基于Schema配置切面
- 【Spring AOP】基于Schema配置切面
- Spring-AOP 基于Schema配置切面
- Spring学习4-面向切面(AOP)之schema配置方式
- 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式
- Spring AOP之切面
- spring-aop配置多切面
- spring之Aop面向切面
- Spring笔记之六:AOP基于@AspectJ配置切面
- Spring AOP( 面向切面编程)配置
- spring添加AOP切面-基于XML配置
- Spring aop切面实际应用 XML配置
- Spring aop切面实际应用 XML配置
- 配置Spring AOP面向切面编程
- 使用Spring(11)使用Spring进行面向切面编程(AOP)Schema-based AOP support
- Spring 切面 AOP基础 之二
- Spring 切面 AOP基础 之三
- java基础[1]final关键字
- 将python代码和注释分离
- 前搜房网副CTO曹艳白干了件大事!
- (一)网络协议简单了解
- RxJava 和 Retrofit使用详解动态拼接一个url和两个url时post请求
- Spring AOP 之 Schema配置切面
- 安卓字体设置
- 安卓 as 4.4机型 Logcat无法输出日志
- 透过现象看本质:共享单车之摩拜和ofo的工作原理分析
- 高通平台 lcd driver 调试小结
- 51NOD 1067 Bash游戏 V2 (找规律)
- [BZOJ 2303][Apio2011]方格染色:并查集
- LDAP网络用户账户
- unity 脚本实现UI滑出效果