Pointcut is another core AOP concept, usually appearing as an expression, and allowing you to match certain program execution points to apply an advice. In classic Spring AOP, pointcuts are also declared as Spring beans by using pointcut classes.
Spring provides a family of pointcut classes for you to match program execution points. You can simply declare beans of these types in your bean configuration file to define pointcuts. However, if you find that the built-in pointcut classes cannot satisfy your needs, you can write your own by extending StaticMethodMatcherPointcut or DynamicMethodMatcherPointcut. The former matches execution points by the static class and method information only, while the latter matches them by the dynamic argument values as well.
How It Works:
we used all codes except of configuration file in previous article.
Method Name Pointcuts:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="arithmeticCalculator" class="cn.cgw.aop.ArithmeticCalculatorImpl"/> <bean id="unitCalculator" class="cn.cgw.aop.UnitCalculatorImpl"/> <!-- Advice --> <bean id="loggingBeforeAdvice" class="cn.cgw.aop.LoggingBeforeAdvice"/> <bean id="loggingAfterAdvice" class="cn.cgw.aop.LoggingAfterAdvice"/> <bean id="loggingThrowsAdvice" class="cn.cgw.aop.LoggingThrowsAdvice"/> <bean id="loggingAroundAdvice" class="cn.cgw.aop.LoggingAroundAdvice"/> <!-- PointCut --> <bean id="methodNamePointcut" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedNames"> <list> <value>add</value> <value>div</value> </list> </property> </bean> <!-- Advisor --> <bean id="methodNameAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="methodNamePointcut"/> <property name="advice" ref="loggingAroundAdvice"/> </bean> <!-- AOP Proxy --> <bean id="arithmeticCalculatorProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- <property name="proxyInterfaces"> <list> <value>cn.cgw.aop.ArithmeticCalculator</value> </list> </property> --> <property name="target" ref="arithmeticCalculator"/> <property name="interceptorNames"> <list> <value>methodNameAdvisor</value> </list> </property> </bean> </beans>
Regular Expression Pointcuts:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="arithmeticCalculator" class="cn.cgw.aop.ArithmeticCalculatorImpl"/> <bean id="unitCalculator" class="cn.cgw.aop.UnitCalculatorImpl"/> <!-- Advice --> <bean id="loggingBeforeAdvice" class="cn.cgw.aop.LoggingBeforeAdvice"/> <bean id="loggingAfterAdvice" class="cn.cgw.aop.LoggingAfterAdvice"/> <bean id="loggingThrowsAdvice" class="cn.cgw.aop.LoggingThrowsAdvice"/> <bean id="loggingAroundAdvice" class="cn.cgw.aop.LoggingAroundAdvice"/> <!-- Regular expression pointcut --> <bean id="regexpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="patterns"> <list> <value>.*mul.*</value> <value>.*div.*</value> </list> </property> <property name="advice" ref="loggingAroundAdvice"/> </bean> <!-- AOP Proxy --> <bean id="arithmeticCalculatorProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- <property name="proxyInterfaces"> <list> <value>cn.cgw.aop.ArithmeticCalculator</value> </list> </property> --> <property name="target" ref="arithmeticCalculator"/> <property name="interceptorNames"> <list> <value>regexpAdvisor</value> </list> </property> </bean> </beans>