使用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.

最新文章

  1. Salesforce ADM201备考心得
  2. 170106、用9种办法解决 JS 闭包经典面试题之 for 循环取 i
  3. git初学习体会
  4. Redis 分布式session
  5. css3创建一个上下线性渐变色背景的div
  6. XE6移动开发环境搭建之IOS篇(9):配置XE6的IOS SDK(有图有真相)
  7. sql语句将本地服务器中的数据插入到外网服务器中
  8. echarts样式修改
  9. Linxu服务器上安装JDK小白教程
  10. STM32 STOP模式唤醒后的时钟
  11. [nodejs] nodejs开发个人博客(六)数据分页
  12. TensorFlow tf.app&tf.app.flags用法介绍
  13. JS基础-运算符-函数
  14. WP8.1学习系列(第二十七章)——ListView和GridView入门
  15. 函数使用六:ABAP4_CALL_TRANSACTION
  16. maven学习之一:maven安装
  17. cdoj841-休生伤杜景死惊开 (逆序数变形)【线段树 树状数组】
  18. C# 依据鼠标坐标取网页内成员坐标.ie
  19. Sql 中取小数点后面两位小数
  20. git身份验证失败清除密码缓存

热门文章

  1. Eclipse,myeclipse安装 配置Maven
  2. ITerms2在mac系统下的安装和配色,并和go2shell关联
  3. python2和python3中的编码问题
  4. box-shadow用法简介
  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统
  6. 原生js编写设为首页兼容ie、火狐和谷歌
  7. Codeforces 776 A.Serial Killer-String直接比较是否相同
  8. HDU 6081 度度熊的王国战略【并查集/数据弱水题/正解最小割算法】
  9. office中密码的移除方法
  10. Codeforces 785E Anton and Permutation(分块)