Spring AOP使用配置介绍(二):切面的配置
来源:互联网 发布:tda分析软件正版费用 编辑:程序博客网 时间:2024/04/26 07:18
在文章(一)中已经介绍了增强的使用,此篇介绍切面的配置方法。
创建静态普通方法名匹配的切面:
该类中的matches方法用来确定哪些方法需要执行增强中的内容;getClassFilter则确定哪些类需要被代理,这个方法在使用自动创建代理时会很有用。
在spring配置文件中配置切面:
其中loggerBeforeAdvice是(一)中已经定义过的增强。
配置代理类:
注意此处配置与(一)中只有interceptorNames换成了我们刚刚配置的那个切面。
此外也可以使用静态正则表达式方法匹配来创建切面,代码如下
其中.*get.*是标准的正则表达式语言,它匹配那些名称种包含get的方法
通过上面的步骤我们就定义完了切面了。
创建静态普通方法名匹配的切面:
package com.maxjay.bank.advice;import java.lang.reflect.Method;import org.springframework.aop.ClassFilter;import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;import com.maxjay.bank.service.UserService;/** * 静态普通方法名匹配切面定义 * @author Administrator * */public class ValidateUserAdvisor extends StaticMethodMatcherPointcutAdvisor {/** * */private static final long serialVersionUID = 1L;/** * 只对validateUser进行拦截 */public boolean matches(Method method, Class targetClass) {// 此次可以使用正则表达式来匹配return "validateUser".equals(method.getName());}public ClassFilter getClassFilter() {return new ClassFilter() {public boolean matches(Class clazz) {return UserService.class.isAssignableFrom(clazz);}};}}
该类中的matches方法用来确定哪些方法需要执行增强中的内容;getClassFilter则确定哪些类需要被代理,这个方法在使用自动创建代理时会很有用。
在spring配置文件中配置切面:
<bean id="validateUserAdvisor" class="com.maxjay.bank.advice.ValidateUserAdvisor"><property name="advice"><ref bean="loggerBeforeAdvice" /></property></bean>
其中loggerBeforeAdvice是(一)中已经定义过的增强。
配置代理类:
<!--配置单个bean的代理,在使用时不能用原有bean的id要用AppContext.get("singleLoginProxy")从context中获取(见测试类LoggerAdviceTest)--><bean id="singleLoginProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces"><value>com.maxjay.bank.service.UserService</value></property><property name="interceptorNames"><list> <value>validateUserAdvisor</value> </list></property><property name="target"><ref bean="userService" /></property><!-- 设置是否直接代理类(默认为false):true即使用cglib生成代理类,此时target对象不可以是JDK动态代理过的bean;false则使用JDK动态代理 --><property name="proxyTargetClass"><value>true</value></property></bean>
注意此处配置与(一)中只有interceptorNames换成了我们刚刚配置的那个切面。
此外也可以使用静态正则表达式方法匹配来创建切面,代码如下
<!-- 定义静态正则表达式方法名匹配切面 --><bean id="regexpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name="patterns"><list><value>.*get.*</value></list></property><property name="advice"><ref bean="loggerBeforeAdvice" /></property></bean>
其中.*get.*是标准的正则表达式语言,它匹配那些名称种包含get的方法
通过上面的步骤我们就定义完了切面了。
0 0
- Spring AOP使用配置介绍(二):切面的配置
- Spring配置过程 (二)面向切面编程AOP
- Spring AOP使用配置介绍(一):增强的配置
- spring-aop配置多切面
- Spring aop 使用aspectjrt配置切面+ehcache简单配置
- Spring框架-AOP细节2(XML配置AOP切面)
- Spring AOP使用配置介绍(五):基于Schema配置的aop
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- Spring AOP(二)——在XML中配置切面
- 利用SPring AOP配置切面的一个例子
- 关于spring的aop(切面编程)配置总结
- Spring AOP学习笔记(4):AOP切面优先级&切面表达式重用&xml配置方式
- Spring AOP( 面向切面编程)配置
- 【Spring AOP】基于Schema配置切面
- 【Spring AOP】基于Schema配置切面
- spring添加AOP切面-基于XML配置
- android五大布局
- 亚投行对“中国银行”的影响
- Linux下 将VIM的TAB设置为4个空格
- hadoop 伪分布式的安装
- Spring AOP使用配置介绍(一):增强的配置
- Spring AOP使用配置介绍(二):切面的配置
- Java并发编程:volatile关键字解析
- Spring AOP使用配置介绍(三):自动创建代理
- Spring AOP使用配置介绍(四):基于@AspectJ注解的aop
- 只用逻辑运算和移位运算求两个数的平均数
- Spring AOP使用配置介绍(五):基于Schema配置的aop
- Oracle会话数量查询及结束会话方法
- Oracle使用技巧
- ios多线程操作(十)—— 用NSOperation开启多线程操作