【Spring基础】AOP使用XML实战

前言

Github:https://github.com/yihonglei/thinking-in-spring(spring-aop-xml工程)

AOP是什么,有哪些概念?

参考: Spring使用注解AOP实战

Spring使用注解AOP实战

1、 XML中aop配置元素分析

在进行XML配置aop之前,需要了解spring的aop命名空间中,

提供了多少个元素用来在XML中声明切面。

这些配置在以下xml中体会。

2、定义切面

创建一个切面,该切面没有任何的注解。

package com.jpeony.spring.aop;

/**
 *  切面类
 */
public class PersonAspect {

    /**
     * 开会之前--找个位置坐下
     */
    public void takeSeats() {
        System.out.println("找位置坐");
    }

    /**
     * 开会之前--手机调成静音
     */
    public void silenceCellPhones() {
        System.out.println("手机调成静音");
    }

    /**
     * 开会之后--写会议总结报告
     */
    public void summary() {
        System.out.println("写会议总结报告");
    }

}

3、创建目标类

接口:

package com.jpeony.spring.aop;

public interface ConferenceService {
    void conference();
}

实现:

package com.jpeony.spring.aop;

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.jpeony.spring.aop.ConferenceServiceImpl"/>

    <!-- 切面类 -->
    <bean id="person" class="com.jpeony.spring.aop.PersonAspect"/>

    <!--
       在<aop:config>中我们可以声明一个活多个通知器、切面或者切点。
     -->
    <aop:config>
        <aop:aspect ref="person">
            <!-- 前置通知 -->
            <aop:before pointcut="execution(* com.jpeony.spring.aop.ConferenceServiceImpl.conference(..))"
                        method="takeSeats" />
            <!-- 前置通知 -->
            <aop:before pointcut="execution(* com.jpeony.spring.aop.ConferenceServiceImpl.conference(..))"
                        method="silenceCellPhones" />
            <!-- 后置通知 -->
            <aop:after pointcut="execution(* com.jpeony.spring.aop.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.jpeony.spring.aop.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.jpeony.spring;

import com.jpeony.spring.aop.ConferenceService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopTest {

    @Test
    public void testAopXml() {
        // 根据配置文件创建IOC容器
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        // 从容器中获取Bean
        ConferenceService conferenceService = (ConferenceService) context.getBean("conferenceService");
        // 调用Bean方法
        conferenceService.conference();
    }

}

6、运行结果

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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值