AOP的动态代理配置文件以及 其test(一)

在这个配置文件中需要修改的几个地方:
1、 xmlns:aop=“http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd

2、在这个里面需要理解的点
* 连接点,就是所有的实现类中的方法
* 核心代码:就是在一个程序中必须被执行的代码
* 切入点:就是需要被强调的点
* 通知/增强点 :就是前置通知和后置通知
* 切入面:当通知点/增强点和切入点结合之后就变成了切入面,也就是被增强的部分。

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

    <!--
         AOP
            1、目标类
            2、增强类
            3、织入
    -->
    <!-- 目标类 -->
    <bean id="bocDao" class="com.whpu.k16035.aop.dao.impl.BOcDaoImpl"></bean>
    <!--增强类 -->
    <bean id="security" class="com.whpu.k16035.aop.security.Security"></bean>
    <bean id="logger" class="com.whpu.k16035.aop.log.Logger"></bean>
    <bean id="cc" class="com.whpu.k16035.aop.cache.ClearCache"></bean>

    <!--aop织入-->
    <aop:config>
        <!--配置切入点   要被增强的方法
             id:切入点的名称
             expression : 切入点表达式
        -->
        <aop:pointcut id="pointCut" expression="execution(* com.whpu.k16035.aop.dao.impl.*.*(..) )"></aop:pointcut>
        <!--切面
           前置切面中:
           ref:引入增强类/通知类
           order:前置通知,值越小越先执行
                  后置通知,值越大越先执行
        -->
        <!--配置安全模块切面-->
        <aop:aspect ref="security" order="1">
            <!--前面通知-->
            <aop:before method="isSecurity" pointcut-ref="pointCut"></aop:before>
        </aop:aspect>

        <!--配置日志管理模块切面-->
        <aop:aspect ref="logger" order="2">
            <!--后置通知-->
            <aop:after method="log" pointcut-ref="pointCut"></aop:after>
        </aop:aspect>

        <!--配置缓存管理模块切面-->
        <aop:aspect ref="cc" order="1">
            <!--后置通知-->
            <aop:after method="clear" pointcut-ref="pointCut"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

test代码

@Test
    public void proxyAopTest(){
        //动态代理
        ApplicationContext app = new ClassPathXmlApplicationContext("aop/beans_aop..xml");
        BOcDao bOcDao = (BOcDao)app.getBean("bocDao");
        bOcDao.select();
    }

猜你喜欢

转载自blog.csdn.net/qq_43479839/article/details/92816832
今日推荐