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

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

使用 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(" 结束事务 ");

}

}

在调用业务接口方法之前,我要执行 validateUser beginTransaction 方法,调用玩业务接口方法之后,执行 endTransaction writeLogInfo 方法。

另外,我们发现,这个切面是一个 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 表示修饰符如 public protected 等, 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

结束事务

    书写日志信息