achieve aop through xml

前端之家收集整理的这篇文章主要介绍了achieve aop through xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

The main way to achive AOP is deploying a xml file。

Now a xml file is presented to be a explanation .

  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:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  12. <context:annotation-config />
  13. <context:component-scan base-package="com.bjsxt"/>
  14. <bean id="logInterceptor" class ="com.bjsxt.aop.LogInterceptor"></bean>
  15. <aop:config>
  16. <aop:pointcut
  17. expression="execution(public * com.bjsxt.service..*.add(..))"
  18. id="servicePointcut"/>
  19. <aop:aspect id="logAspect" ref="logInterceptor">
  20. <aop:before method="before" pointcut-ref="servicePointcut" />
  21. </aop:aspect>
  22. </aop:config>
  23.  
  24. </beans>

The tag <aop:config> declares that we will deploy a aop now.

While,I declare a "pointcut" at first. It is obvIoUs that it's a globe pointcut and it works for every aspect behind it .

"expression="execution(public * com.bjsxt.service..*.add(..))" shows the pointcut represent all method like “add()” in the classes of the package named"com.bjsxt.service.."

the pointcut‘s id is “servicePointcut” in addition。


Next, a aspect is declared whose name is “logAspect” 。 (nobody care what name it has in this situation)。

  1. pointcut-ref="servicePointcut" shows that it is a aspect for the pointcut named servicePointcutwhich we had created a moment ago.
  1. Its ref = <span style="font-family: Arial,Helvetica,sans-serif;">"logInterceptor" so we can find a bean named logInterceptor to know what methods the aspect hasAnd according to the sentence “<aop:before method</span>
  1. <span style="font-family: Arial,sans-serif;">="before" ",I think we must do the method named "before" in "logIntercepor " as the type of" before method". It means the method woulb be done before pointcut.</span>
  1. <span style="font-family: Arial,sans-serif;">
  2. </span>
  1. <span style="font-family: Arial,sans-serif;">We can also complete the xml file in design mode。 It looks much more clear.</span>
  1. <span style="font-family: Arial,sans-serif;">
  2. </span>

猜你在找的XML相关文章