Spring AOP之aop:config

本文介绍Spring框架中AOP模块的新XML配置方法,通过使用特定的命名空间简化了配置过程,使配置更为直观和简洁。

Spring在某个版本之后开始支持一组新的xml标签用来简化配置,如context,aop,mvc等,下面我们来看看aop标签的配置方法:

接上篇,业务类和通知类都不变,配置这样写:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

  <!--业务bean-->
  <bean id="someBiz" class="org.lanqiao.springDemo.aop.SomeBizImpl"/>

  <bean id="advice1" class="org.lanqiao.springDemo.aop.Advice1" />
  <!--aop标签支持-->
  <aop:config>
    <!--切入点表达式-->
    <aop:pointcut id="bizes" expression="execution( * org.lanqiao.springDemo..*Biz*.*Biz(..))"/>
    <!--组合通知与切入点-->
    <aop:advisor advice-ref="advice1" pointcut-ref="bizes"/>
  </aop:config>

</beans>

其实所有的AOP配置方法都有两个核心点:切入点和通知,换汤不换药。

测试代码:

package org.lanqiao.springDemo.aop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration("classpath:aop2.xml")
public class SomeBizImplTest2 extends AbstractJUnit4SpringContextTests {

  @Autowired
  private SomeBiz someBiz;

  @org.junit.Test
  public void addBizUsingBean() throws Exception {
    someBiz.addBiz();
  }

  @org.junit.Test
  public void other() throws Exception {
    someBiz.other();
  }

}

相比之前,新的xml配置标签语义更加明确,也更简单了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值