• Srping AOP xml方式


     

    使用aop需要:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


        <!--  <context:annotation-config />  -->
        <context:component-scan base-package="com.bjsxt"/>

     

     

     

     

        <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
        <aop:config>

     

            <aop:pointcut

                expression=“execution(public * com.bjsxt.service..*.add(..))"

                id=“servicePointcut”/>

        <!--  pointcut可以定义在aspect里面,也可以定义在它外边,调用pointcut的时候可以写pointcut-ref=“” –>


            <aop:aspect id="logAspect" ref="logInterceptor">
                <aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" />
            </aop:aspect>
           

        </aop:config>

     

     

     

     

     

     

    声明一个切面类:

    Declaring an aspect

    Using the schema support, an aspect is simply a regular Java object defined as a bean in your Spring application context. The state and behavior is captured in the fields and methods of the object, and the pointcut and advice information is captured in the XML.

    An aspect is declared using the <aop:aspect> element, and the backing bean is referenced using the ref attribute:

     

    <aop:config>
      <aop:aspect id="myAspect" ref="aBean">
        ...
      </aop:aspect>
    </aop:config>
    
    <bean id="aBean" class="...">
      ...
    </bean>
     

    The bean backing the aspect ("aBean" in this case) can of course be configured and dependency injected just like any other Spring bean.

     

     

    定义一个pointcut:

    Declaring a pointcut

    A named pointcut can be declared inside an <aop:config> element, enabling the pointcut definition to be shared across several aspects and advisors.

    A pointcut representing the execution of any business service in the service layer could be defined as follows:

    <aop:config>
    
      <aop:pointcut id="businessService"
            expression="execution(* com.xyz.myapp.service.*.*(..))"/>
    
    </aop:config>

    Note that the pointcut expression itself is using the same AspectJ pointcut expression language as described in Section 9.2, “@AspectJ support”. If you are using the schema based declaration style with Java 5, you can refer to named pointcuts defined in types (@Aspects) within the pointcut expression, but this feature is not available on JDK 1.4 and below (it relies on the Java 5 specific AspectJ reflection APIs). On JDK 1.5 therefore, another way of defining the above pointcut would be:

    <aop:config>
    
      <aop:pointcut id="businessService"
            expression="com.xyz.myapp.SystemArchitecture.businessService()"/>
    
    </aop:config>

    Assuming you have a SystemArchitecture aspect as described in the section called “Sharing common pointcut definitions”.

    Declaring a pointcut inside an aspect is very similar to declaring a top-level pointcut:

    <aop:config>
    
      <aop:aspect id="myAspect" ref="aBean">
    
        <aop:pointcut id="businessService"
              expression="execution(* com.xyz.myapp.service.*.*(..))"/>
    
        ...
    
      </aop:aspect>
    
    </aop:config>

    Much the same way in an @AspectJ aspect, pointcuts declared using the schema based definition style may collect join point context. For example, the following pointcut collects the 'this' object as the join point context and passes it to advice:

     

    <aop:config>
    
      <aop:aspect id="myAspect" ref="aBean">
    
        <aop:pointcut id="businessService"
              expression="execution(* com.xyz.myapp.service.*.*(..)) &amp;&amp; this(service)"/>
        <aop:before pointcut-ref="businessService" method="monitor"/>
        ...
    
      </aop:aspect>
    
    </aop:config>
     

    The advice must be declared to receive the collected join point context by including parameters of the matching names:

    public void monitor(Object service) {
        ...
    }

    When combining pointcut sub-expressions, '&&' is awkward within an XML document, and so the keywords 'and', 'or' and 'not' can be used in place of '&&', '||' and '!' respectively. For example, the previous pointcut may be better written as:

    <aop:config>
    
      <aop:aspect id="myAspect" ref="aBean">
    
        <aop:pointcut id="businessService"
              expression="execution(* com.xyz.myapp.service.*.*(..)) and this(service)"/>
        <aop:before pointcut-ref="businessService" method="monitor"/>
        ...
    
      </aop:aspect>
    
    </aop:config>

    Note that pointcuts defined in this way are referred to by their XML id and cannot be used as named pointcuts to form composite pointcuts. The named pointcut support in the schema based definition style is thus more limited than that offered by the @AspectJ style.

  • 相关阅读:
    python+django+vue搭建前后端分离项目
    PC网页js调用本地应用程序
    mysql-connetor-c 自动创建数据库、数据库表的命令
    JMeter设置响应数据的编码格式
    VS2013常用快捷键
    WPF 实现 TextBox 只能输入数字并且不能使用拷贝功能
    WPF 先显示登录成功,验证成功后显示主窗口
    ListControl 设置表格行高与字体
    win7 删除多余启动项的方法
    设置编辑工具UltraEdit的背景色为护眼颜色
  • 原文地址:https://www.cnblogs.com/flying607/p/3486281.html
Copyright © 2020-2023  润新知