Spring AOP + AspectJ in XML configuration example
2015-08-23 14:24
549 查看
For those don’t like annotation or using JDK 1.4, you can use AspectJ in XML based instead.
Review last
1. AspectJ
AspectJ
Equivalent functionality in XML, with
2. AspectJ
AspectJ
Equivalent functionality in XML, with
3. AspectJ
AspectJ
Equivalent functionality in XML, with
4. AspectJ
AspectJ
Equivalent functionality in XML, with
5. AspectJ
AspectJ
Equivalent functionality in XML, with
Full XML example
See complete AspectJ XML based configuration file.
Review last
customerBointerface again, with few methods, later you will learn how to intercept it via AspectJ in XML file.
package com.mkyong.customer.bo; public interface CustomerBo { void addCustomer(); String addCustomerReturnValue(); void addCustomerThrowException() throws Exception; void addCustomerAround(String name); }
1. AspectJ <aop:before> = @Before
AspectJ @Beforeexample.
package com.mkyong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class LoggingAspect { @Before("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))") public void logBefore(JoinPoint joinPoint) { //... } }
Equivalent functionality in XML, with
<aop:before>.
<!-- Aspect --> <bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Before --> <aop:pointcut id="pointCutBefore" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> </aop:aspect> </aop:config>
2. AspectJ <aop:after>
= @After
AspectJ @Afterexample.
package com.mkyong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.After; @Aspect public class LoggingAspect { @After("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))") public void logAfter(JoinPoint joinPoint) { //... } }
Equivalent functionality in XML, with
<aop:after>.
<!-- Aspect --> <bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @After --> <aop:pointcut id="pointCutAfter" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> </aop:aspect> </aop:config>
3. AspectJ <aop:after-returning>
= @AfterReturning
AspectJ @AfterReturningexample.
package com.mkyong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterReturning; @Aspect public class LoggingAspect { @AfterReturning( pointcut = "execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))", returning= "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { //... } }
Equivalent functionality in XML, with
<aop:after-returning>.
<!-- Aspect --> <bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @AfterReturning --> <aop:pointcut id="pointCutAfterReturning" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning" returning="result" pointcut-ref="pointCutAfterReturning" /> </aop:aspect> </aop:config>
4. AspectJ <aop:after-throwing>
= @AfterReturning
AspectJ @AfterReturningexample.
package com.mkyong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterThrowing; @Aspect public class LoggingAspect { @AfterThrowing( pointcut = "execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))", throwing= "error") public void logAfterThrowing(JoinPoint joinPoint, Throwable error) { //... } }
Equivalent functionality in XML, with
<aop:after-throwing>.
<!-- Aspect --> <bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @AfterThrowing --> <aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing" throwing="error" pointcut-ref="pointCutAfterThrowing" /> </aop:aspect> </aop:config>
5. AspectJ <aop:after-around>
= @Around
AspectJ @Aroundexample.
package com.mkyong.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Around; @Aspect public class LoggingAspect { @Around("execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))") public void logAround(ProceedingJoinPoint joinPoint) throws Throwable { //... } }
Equivalent functionality in XML, with
<aop:after-around>.
<!-- Aspect --> <bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Around --> <aop:pointcut id="pointCutAround" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config>
Full XML example
See complete AspectJ XML based configuration file.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy /> <bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" /> <!-- Aspect --> <bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect"> <!-- @Before --> <aop:pointcut id="pointCutBefore" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> <!-- @After --> <aop:pointcut id="pointCutAfter" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> <!-- @AfterReturning --> <aop:pointcut id="pointCutAfterReturning" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning" returning="result" pointcut-ref="pointCutAfterReturning" /> <!-- @AfterThrowing --> <aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing" throwing="error" pointcut-ref="pointCutAfterThrowing" /> <!-- @Around --> <aop:pointcut id="pointCutAround" expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config> </beans>
相关文章推荐
- Spring AOP + AspectJ annotation example
- asp 支付宝 企业版 接口 支持网银接口 ,网银直接支付
- AspectJ在测试中的应用
- 7 天玩转 ASP.NET MVC — 第 6 天
- 7 天玩转 ASP.NET MVC — 第 6 天
- asp.net(C#)利用QRCode生成二维码---.NET菜鸟的成长之路
- asp.net mvc @Html.Raw 作用
- asp.net page 生命周期事件-1
- ASP.Net发布到IIS中出现错误: 处理程序"PageHandlerFactory-Integrated"在其模块列表中有一个错误模块"ManagedPipelineHandler"
- ASP.NET MVC 4 插件化架构简单实现-思路篇
- ASP.NET MVC 4 插件化架构简单实现-思路篇
- ASP.NET Web API实践系列09,在Fiddler和控制台中模拟GET和POST请求
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- Asp.net mvc5 解析route源码实现自己的route系统
- asp.net MVC helper 和自定义函数@functions小结
- asp.net给DropDownList添加ToolTip(title)属性
- Asp.Net 一个请求的处理流程
- 毕业设计 ASP.Net+EasyUI开发 X X露天矿调度管理信息系统(一)
- ASP.NET mvc中session
- ASP.NET——生成验证码