Spring AOP-configuration based on XML files, aop-xml
The configuration of Spring AOP can be based on Annotations or XML files. The previous articles use annotations. The following describes how to configure an XML file.
The used test class and cut class are similar. You only need to remove the annotation that belongs to AOP. The XML configuration of AOP is as follows:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: aop = "http://www.springframework.org/schema/aop" 5 xmlns: context = "http://www.springframework.org/schema/context" 6 xsi: schemaLocation = "http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 7 http://www.springframework.org/schem A/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> 9 10 <! -- Configure bean --> 11 <bean id = "arithmeticCalculator" class = "com. yl. spring. aop. xml. ArithmeticCalculatorImpl"> </bean> 12 13 14 <! -- Configure bean --> 15 <bean id = "loggingAspect" class = "com. yl. spring. aop. xml. loggingAspect "> </bean> 16 17 <bean id =" validationAspect "class =" com. yl. spring. aop. xml. validationAspect "> </bean> 18 19 <! -- Configure AOP --> 20 <aop: config> 21 <! -- Configure the cut expression --> 22 <aop: pointcut expression = "execution (public int com. yl. spring. aop. xml. arithmeticCalculator. *(..)) "id =" pointcut "/> 23 <! -- Configuration aspect and notification --> 24 <aop: aspect ref = "loggingAspect" order = "2"> 25 <aop: before method = "beforeMethod" pointcut-ref = "pointcut"/> 26 <aop: after method = "afterMethod" pointcut-ref = "pointcut"/> 27 <aop: after-throwing method = "afterThrowing" pointcut-ref = "pointcut" throwing = "e"/> 28 <aop: after-returning method = "afterReturning" pointcut-ref = "pointcut" returning = "result"/> 29 30 <aop: around method = "aroundMethod" pointcut-ref = "pointcut"/> 31 </aop: aspect> 32 33 <aop: aspect ref = "validationAspect" order = "1"> 34 <aop: before method = "vlidateArgs" pointcut-ref = "pointcut"/> 35 </aop: aspect> 36 </aop: config> 37 38 </beans>