基于Spring AOP的XML配置

22 篇文章 0 订阅

在本节中要使用AOP的命名空间,所以你需要导入下面spring 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:aop = "http://www.springframework.org/schema/aop"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

   <!-- bean definition & AOP specific configuration -->

</beans>

在你应用程序的CLASSPATH中,你也需要添加AspectJ库的路径。这些库可以在AspectJ的安装路径中的‘lib’目录中活得。除此之外,你还可以从网络中下载这些库:
- aspectjrt.jar
- aspectjweaver.jar
- aspectj.jar
- aopalliance.jar

Declaring an aspect

声明一个切面:一个切面可以使用<aop:aspect>标签来声明,后面的bean可以使用ref来引用。

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

<bean id = "aBean" class = "...">
   ...
</bean>

这里的 aBean 会被配置并依赖注入,就和前面章节中看到的其他Spring bean一样。

Declaring a pointcut

声明一个切入点:一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。在处理基于配置的 XML 架构时,切入点将会按照如下所示定义:

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

<bean id = "aBean" class = "...">
   ...
</bean>

下面的例子定义一个叫 businessService 的切入点,将会匹配Student类中的getName()方法:

<aop:config>
   <aop:aspect id = "myAspect" ref = "aBean">
      <aop:pointcut id = "businessService" 
         expression = "execution(*com.tutorialspoint.Student.getName(..))"/>
         ...
   </aop:aspect>
</aop:config>

<bean id = "aBean" class = "...">
   ...
</bean>

Declaring advices

你可以声明下面五种任何一种的advice:

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

      <!-- a before advice definition -->
      <aop:before pointcut-ref = "businessService" method = "doRequiredTask"/>

      <!-- an after advice definition -->
      <aop:after pointcut-ref = "businessService" method = "doRequiredTask"/>

      <!-- an after-returning advice definition -->
      <!--The doRequiredTask method must have parameter named retVal -->
      <aop:after-returning pointcut-ref = "businessService"
         returning = "retVal" method = "doRequiredTask"/>

      <!-- an after-throwing advice definition -->
      <!--The doRequiredTask method must have parameter named ex -->
      <aop:after-throwing pointcut-ref = "businessService"
         throwing = "ex" method = "doRequiredTask"/>

      <!-- an around advice definition -->
      <aop:around pointcut-ref = "businessService" method = "doRequiredTask"/>
      ...
   </aop:aspect>
</aop:config>

<bean id = "aBean" class = "...">
  ...
</bean>

你可以使用同一个doRequiredTask方法,或者为不同的advices使用不同的方法。这些方法将会被定义在aspect模块的一部分。

Example

Student.java

package com.soygrow.XmlAop;

public class Student {
    private Integer age;
    private String name;

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        System.out.println("Age : " + age);
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        System.out.println("Name : " + name);
        return name;
    }

    public void printThrowException() {
        System.out.println("Exception raised");
        throw new IllegalArgumentException();
    }
}

Logging.java

package com.soygrow.XmlAop;

public class Logging {
    /**
     * This is the method which I would like to execute
     * before a selected method execution.
     */
    public void beforeAdvice() {
        System.out.println("Going to setup stedent profile.");
    }

    /**
     * This is the method which I would like to execute
     * when any method returns.
     */
    public void afterAdvice() {
        System.out.println("Stedent profile has been setup.");
    }

    /**
     * This is the method which I would like to execute
     * when any method returns.
     */
    public void afterReturningAdvice(Object retVal) {
        System.out.println("Returning: " + retVal.toString());
    }

    /**
     * This is the method which I would like to execute
     * if there is an exception raised.
     */
    public void AfterThrowingAdvice(IllegalArgumentException ex) {
        System.out.println("There has been an exception: " + ex.toString());
    }
}

MainApp.java

package com.soygrow.XmlAop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("XmlAopBeans.xml");

        Student student = (Student) context.getBean("student");
        student.getName();
        student.getAge();
        student.printThrowException();
    }
}

XmlAopBeans.xml

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:config>
        <aop:pointcut id="selectAll" expression="execution(* com.soygrow.XmlAop.Student.*(..))"/>
        <aop:aspect id="log" ref="logging">
            <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
            <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
            <aop:after-returning pointcut-ref="selectAll" returning="retVal" method="afterReturningAdvice"/>
            <aop:after-throwing pointcut-ref="selectAll" throwing="ex" method="AfterThrowingAdvice"/>
        </aop:aspect>
    </aop:config>

    <bean id="logging" class="com.soygrow.XmlAop.Logging"/>

    <bean id="student" class="com.soygrow.XmlAop.Student">
        <property name="age" value="11"/>
        <property name="name" value="Zara"/>
    </bean>
</beans>

如果一切正常,运行结果:

Going to setup stedent profile.
Name : Zara
Stedent profile has been setup.
Returning: Zara
Going to setup stedent profile.
Age : 11
Stedent profile has been setup.
Returning: 11
Going to setup stedent profile.
Exception raised
Stedent profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
遇到的问题
  • 切记,要把上面提到的四个jar的库添加进去,不然会报错:Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0'
  • aop:pointcutexecution中内容不知道是怎么写的,这个问题我在下面会进行补充
  • 运行最后程序还是会抱异常,是因为我们在代码中主动抛出异常,这个可以不用管
常见的切点表达式
  • 匹配方法签名
// 匹配指定包中的所有的方法
execution(* com.xys.service.*(..))

// 匹配当前包中的指定类的所有方法
execution(* UserService.*(..))

// 匹配指定包中的所有 public 方法
execution(public * com.xys.service.*(..))

// 匹配指定包中的所有 public 方法, 并且返回值是 int 类型的方法
execution(public int com.xys.service.*(..))

// 匹配指定包中的所有 public 方法, 并且第一个参数是 String, 返回值是 int 类型的方法
execution(public int com.xys.service.*(String name, ..))
  • 匹配类型签名
// 匹配指定包中的所有的方法, 但不包括子包
within(com.xys.service.*)

// 匹配指定包中的所有的方法, 包括子包
within(com.xys.service..*)

// 匹配当前包中的指定类中的方法
within(UserService)

// 匹配一个接口的所有实现类中的实现的方法
within(UserDao+)
  • 匹配Bean名字
// 匹配以指定名字结尾的 Bean 中的所有方法
bean(*Service)
  • 切点表达式组合
// 匹配以 Service 或 ServiceImpl 结尾的 bean
bean(*Service || *ServiceImpl)

// 匹配名字以 Service 结尾, 并且在包 com.xys.service 中的 bean
bean(*Service) && within(com.xys.service.*)

那么基于以上内容,上面例子中aop:pointcut切入点选择com.soygrow.XmlAop.Student中定义的所有方法。你也可以将Student换成,表示XmlAop包下的所有类的所有方法,同时也可以缩小你的切入点使用确切的类以及类中的方法来替代,下面需要修改上面的XML配置表示将类中的一个方法作为切入点:

<aop:pointcut id="selectAll" expression="execution(* com.soygrow.XmlAop.Student.getName(..))"/>

如果一切正常,运行的结果是:

Going to setup stedent profile.
Name : Zara
Stedent profile has been setup.
Returning: Zara
Age : 11
Exception raised

如果将getName修改成setName,运行结果是:

Name : Zara
Age : 11
Exception raised
代码分析
  • 将切入点修改为类中的一个方法的时候,只会在调用那个方法的时候才会执行logging中相应的通知,因为根据XML的配置的aop:aspect只是针对那一个方法的
  • 同理,将类中的方法设置成setName()时,aop:aspect配置没有起作用的时候,因为该方法没有执行,所以运行结果只有那些内容
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值