spring06

时间:2020-03-09 21:01:28   收藏:0   阅读:52

title: spring06
date: 2020-03-09 19:31:51
tags:该部分记录了AOP


记录了AOP,有方法执行前,方法执行后,方法环绕。

1、概述

摘自秦老师...

什么是AOP?

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

AOP在Spring中的作用:

提供声明式事务;允许用户自定义切面

2、练习的环境

3、实现的方式一

1、所需类

定义两个需要执行的函数:

2、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
                           https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--    配置bean-->
    <bean id="serviceImp" class="com.nevesettle.service.UserServiceImp"/>
    <bean id="beforeLog" class="com.nevesettle.log.BeforeLog"/>
    <bean id="afterLog" class="com.nevesettle.log.AfterLog"/>

    <!--第一种方式,配置AOP-->
        <aop:config>
        <!--        切入点,execution是固定的表达式-->
        <aop:pointcut id="myPointcut" expression="execution(* com.nevesettle.service.UserServiceImp.*(..))"/>

        <!--        执行环绕-->
        <aop:advisor advice-ref="afterLog" pointcut-ref="myPointcut"/>
        <aop:advisor advice-ref="beforeLog" pointcut-ref="myPointcut"/>
    </aop:config>



</beans>

4、实现的方式二

只需要一个类即可,该类中有要执行的各个方法。

package com.nevesettle.diy;

public class DiyLog {
    public void after(){
        System.out.println("=======方法执行后=======");
    }
    public void before(){
        System.out.println("=======方法执行前=======");
    }
}

然后再在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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    配置bean-->
    <bean id="serviceImp" class="com.nevesettle.service.UserServiceImp"/>
    <bean id="beforeLog" class="com.nevesettle.log.BeforeLog"/>
    <bean id="afterLog" class="com.nevesettle.log.AfterLog"/>

<!--    第二种方式-->
    <bean id="diylog" class="com.nevesettle.diy.DiyLog"/>
<!--    开始配置AOP-->
    <aop:config>
        <aop:aspect ref="diylog">
<!--            切入点-->
            <aop:pointcut id="myPointcut" expression="execution(* com.nevesettle.service.UserServiceImp.*(..))"/>
<!--            通知-->
            <aop:after method="after" pointcut-ref="myPointcut"/>
            <aop:before method="before" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>

</beans>
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!