聊聊程序-AOP之XML实现篇

Posted 小韩聊程序

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聊聊程序-AOP之XML实现篇相关的知识,希望对你有一定的参考价值。

spring.xml


<!-- AOP --><bean id="test_xml2aop_advice" class="cn.shh.test.maven_ssm.module.aop.xml2aop.Xml2AopAdvice"> <property name="maxRetries" value="3"/> <property name="order" value="100"/></bean><bean id="test_xml2aop_service" class="cn.shh.test.maven_ssm.module.aop.xml2aop.Xml2AopServieImpl"/><aop:config proxy-target-class="true"> <!-- 1、声明一个方面 --> <aop:aspect id="test_xml2aop_aspect" ref="test_xml2aop_advice"> <!-- 2、声明切入点 --> <aop:pointcut id="test_xml2aop_pointcut" expression="execution(* cn.shh.test.maven_ssm.module.aop.xml2aop.Xml2AopServie.*())"/> <!-- 3、声明通知 --> <aop:before pointcut-ref="test_xml2aop_pointcut" method="before" /> <aop:after pointcut-ref="test_xml2aop_pointcut" method="after" /> <aop:after-returning pointcut-ref="test_xml2aop_pointcut" method="afterReturning"/> <aop:after-throwing pointcut-ref="test_xml2aop_pointcut" method="afterThrowing"/> <aop:around pointcut-ref="test_xml2aop_pointcut" method="aroundAdvice" /> </aop:aspect></aop:config>


Xml2AopAdvice.java


public class Xml2AopAdvice implements Ordered { private static final int DEFAULT_MAX_RETRIES = 2; private int maxRetries = DEFAULT_MAX_RETRIES; private int order = 1;
public void setMaxRetries(int maxRetries) { this.maxRetries = maxRetries; } public void setOrder(int order) { this.order = order; } @Override public int getOrder() { return this.order; }

void before(){ System.out.println("--- xml before advice ---"); } void after(){ System.out.println("--- xml after advice ---"); } void afterReturning(){ System.out.println("--- xml afterReturn advice ---"); } void afterThrowing(){ System.out.println("--- xml throwing advice ---"); } Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{ int numAttempts = 0; PessimisticLockingFailureException lockFailureException; do { numAttempts++; try { return pjp.proceed(); } catch(PessimisticLockingFailureException ex) { lockFailureException = ex; } } while(numAttempts <= this.maxRetries); throw lockFailureException; }}


Xml2AopSerice.java


public interface Xml2AopServie { void info();}


Xml2AopSericeImpl.java



public class Xml2AopServieImpl implements Xml2AopServie{ @Override public void info() { System.out.println("Xml2AopServie's info method is runing."); }}


App.java


public class App { public static void main(String[] args) {        ApplicationContext cpContext = new ClassPathXmlApplicationContext("spring.xml"); Xml2AopServie servie = (Xml2AopServie) cpContext.getBean("test_xml2aop_service"); servie.info(); }}

以上是关于聊聊程序-AOP之XML实现篇的主要内容,如果未能解决你的问题,请参考以下文章

一起学Spring之注解和Schema方式实现AOP

JAVA之AOP

聊聊Netty那些事儿之Reactor在Netty中的实现(创建篇)

菜鸟学习Spring——60s配置XML方法实现简单AOP

Java开发Spring之AOP详解(xml--注解->方法增强事务管理(声明事务的实现))

七AOP注解