深入理解Spring AOP实战(基于XML)

来源:互联网 发布:域名备案要钱吗 编辑:程序博客网 时间:2024/05/18 01:23
AOP是什么,有哪些概念?
参考: <深入理解Spring AOP实战(基于注解)>
1. XML中aop配置元素分析
在进行XML配置aop之前,需要了解spring的aop命名空间中,
提供了多少个元素用来在XML中声明切面。直接上图看看:
这些配置在以下xml中体会。

2. 定义切面
创建一个切面,该切面没有任何的注解。
package com.lanhuigu.spring;/** *  切面类 */public class Person {    /**     * 开会之前--找个位置坐下     */    public void takeSeats() {        System.out.println("找位置坐");    }    /**     * 开会之前--手机调成静音     */    public void silenceCellPhones() {        System.out.println("手机调成静音");    }    /**     * 开会之后--写会议总结报告     */    public void summary() {        System.out.println("写会议总结报告");    }}

3. 创建目标类
接口:
package com.lanhuigu.spring;public interface ConferenceService {    void conference();}

实现:
package com.lanhuigu.spring;public class ConferenceServiceImpl implements ConferenceService {    @Override    public void conference() {        System.out.println("开会......");    }}

4. XML中声明切面
spring的applicationContext.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: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.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">    <!-- 启用AspectJ自动代理 -->    <aop:aspectj-autoproxy proxy-target-class="true"/>    <!-- bean定义 -->    <bean id="conferenceService" class="com.lanhuigu.spring.ConferenceServiceImpl"/>    <!-- 切面类 -->    <bean id="person" class="com.lanhuigu.spring.Person"/>    <!--       在<aop:config>中我们可以声明一个活多个通知器、切面或者切点。     -->    <aop:config>        <aop:aspect ref="person">            <!-- 前置通知 -->            <aop:before pointcut="execution(* com.lanhuigu.spring.ConferenceServiceImpl.conference(..))"                        method="takeSeats" />            <!-- 前置通知 -->            <aop:before pointcut="execution(* com.lanhuigu.spring.ConferenceServiceImpl.conference(..))"                        method="silenceCellPhones" />            <!-- 后置通知 -->            <aop:after pointcut="execution(* com.lanhuigu.spring.ConferenceServiceImpl.conference(..))"                        method="summary" />        </aop:aspect>    </aop:config>    <!--       在上面的配置中,切入点的定义是一样的,我们看到execution中的内容都是一样,       我们可以使用类似@Pointcut注解方式的元素<aop:pointcut>将其提出为公用部分,       而使用切点表达式的地方通过pointcut-ref引入。优化之后效果如下。     -->    <!--<aop:config>        <aop:aspect ref="person">            <!– 定义切点 –>            <aop:pointcut id="conference"                          expression="execution(* com.lanhuigu.spring.ConferenceServiceImpl.conference(..))"/>            <!– 前置通知 –>            <aop:before pointcut-ref="conference"                        method="takeSeats" />            <!– 前置通知 –>            <aop:before pointcut-ref="conference"                        method="silenceCellPhones" />            <!– 后置通知 –>            <aop:after pointcut-ref="conference"                       method="summary" />        </aop:aspect>    </aop:config>--></beans>

5. 测试类
package com.lanhuigu.spring;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAopXml {    @Test    public void testAopXml() {        // 根据配置文件创建spring容器        ApplicationContext context =                new ClassPathXmlApplicationContext("applicationContext.xml");        // 从容器中获取Bean        ConferenceServiceImpl conferenceService = (ConferenceServiceImpl)context.getBean("conferenceService");        // 调用Bean方法        conferenceService.conference();    }}

6. 运行结果
找位置坐
手机调成静音
开会......
写会议总结报告