AOP学习——配置Spring AOP【1】,使用xml文件

本文介绍如何使用 Spring AOP 进行面向切面编程。通过一个简单实例展示了如何利用 XML 和注解配置 AOP,包括定义切入点、前置通知、后置通知等关键概念。

使用 Spring AOP ,除了 spring.jar 这个包外,还需要 aspectjweaver.jar 。将这两个 jar 包加到类路径就可以了。

一般使用 Spring AOP 可以通过 xml 文件或者 annotation 实现 AOP 的配置。

用一个简单的例子说明。

首先有一个业务接口 Component

public interface Component {

    public void business();

    public void dobusiness();

}

还有它的实现类 ComponentImpl

public class ComponentImpl implements Component{

    public void business() {

        System.out.println("bussiness");

    }

    public void dobusiness() {

        System.out.println("do business");

    }

}

 

现在我要做一个切面 AspectBean:

public class AspectBean {

public void validateUser()

{

System.out.println(" 执行用户验证 !");

}

public void writeLogInfo()

{

System.out.println(" 书写日志信息 ");

}

public void beginTransaction()

{

System.out.println(" 开始事务 ");

}

public void endTransaction()

{

System.out.println(" 结束事务 ");

}

}

在调用业务接口方法之前,我要执行 validateUserbeginTransaction 方法,调用玩业务接口方法之后,执行 endTransactionwriteLogInfo 方法。

另外,我们发现,这个切面是一个 POJO ,易于测试和方便移植。

 

还有一个主类使用这个接口:

public class Main {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        Component component=(Component)ctx.getBean("component");

        component.business();

    }

}

 

好,现在通过 xml 文件和 annotation 配置 Spring AOP

 

使用 xml 文件配置 AOP

创建一个 xml 文档 beans.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-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >  

  

    <aop:config>

        <aop:aspect id="aspectDemo" ref="aspectBean">

            <aop:pointcut id="somePointcut"

            expression="execution(* annoaop.Component.business*(..))" />

            <aop:before pointcut-ref="somePointcut"

            method="validateUser" />

            <aop:before pointcut-ref="somePointcut"

            method="beginTransaction" />

            <aop:after-returning pointcut-ref="somePointcut"

            method="endTransaction" />

            <aop:after-returning pointcut-ref="somePointcut"

            method="writeLogInfo" />

        </aop:aspect>

    </aop:config>

 

   

    <bean id="aspectBean"

    class="annoaop.AspectBean">

    </bean>

    <bean id="component"

    class="annoaop.ComponentImpl">

    </bean>

</beans>

 

红色部分就是配置 AOP 的地方。

所有的切面都放在 aop:config 标签里面。

使用 aop:aspect 声明一个切面 aspectDemo

使用 aop:pointcut 声明一个切入点, expression 属性需要通过切入点表达式告诉 spring 你的 target 是什么。

切入点表达式的使用规则:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern? )

“ ? ” 号的部分表示可省略的, modifers-pattern 表示修饰符如 publicprotected 等, ret-type-pattern 表示方法返回类型, declaring-type-pattern 代表特定的类, name-pattern 代表方法名称, param-pattern 表示参数, throws-pattern 表示抛出的异常。在切入点表达式中,可以使用 * 来代表任意字符,用 .. 来表示任意个参数。

例子里面 expression="execution(* annoaop.Component.business*(..))" 表示匹配 annoaop.Component 接口里面以 business 开头的所有方法,参数任意,返回类型任意。

注意: spring 会根据 xml 文件里你声明的增强顺序来执行,假如你设置:

            <aop:after-returning pointcut-ref="somePointcut"

            method=" writeLogInfo" />

            <aop:after-returning pointcut-ref="somePointcut"

            method="endTransaction " />

则输出为

书写日志信息

结束事务

而不是原来的

结束事务

书写日志信息

 

运行主方法:

执行用户验证 !

开始事务

bussiness

结束事务

    书写日志信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值