2020-02-27(SpringAOP5个创建方法之第五个)

3、第五个创建方法,配置.xml文件

第五个创建方法和第三个第四个区别还是在与.xml的配置文件,主要区别的是标签上的区别,第五个方法新增了

  <aop:aspect ref="my">
            <aop:before method="before" pointcut-ref="mpc"></aop:before>
            <aop:after method="after" pointcut-ref="mpc"></aop:after>
    </aop:aspect>

意思就是调用拦截对象中的指定方法,并且使用pointcut-ref和被代理对象关联起来,完整的配置文件是

<?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">


    <bean id="us" class="com.gaojian.aop5.UserServiceImpl"></bean>
    <bean id="my" class="com.gaojian.aop5.MyAspect"></bean>

    <aop:config proxy-target-class="true">
        <aop:aspect ref="my">
            <aop:pointcut id="mpc" expression="execution(* com.gaojian.aop5.*.*(..))"/>
<!--            选择拦截器对象中的方法,并且引用到mpc,也就是被代理对象上-->
            <aop:before method="before" pointcut-ref="mpc"></aop:before>
            <aop:after method="after" pointcut-ref="mpc"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

测试类为

package com.gaojian.aop5;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop3 {
    @Test
    public void test(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("aop5.xml");
        IUserService us = ac.getBean("us", IUserService.class);
        us.deleteUser(1);
        us.getAllUser();
        us.updateUser(new Object());
        us.saveUser(new Object());
    }
}

执行结果是:


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本博中关于spring的文章:Spring IOC和AOP原理,Spring事务原理探究,Spring配置文件属性...
    Maggie编程去阅读 9,576评论 0 34
  • 本章内容: 面向切面编程的基本原理 通过POJO创建切面 使用@AspectJ注解 为AspectJ切面注入依赖 ...
    谢随安阅读 8,549评论 0 9
  • 对于java中的思考的方向,1必须要看前端的页面,对于前端的页面基本的逻辑,如果能理解最好,不理解也要知道几点。 ...
    神尤鲁道夫阅读 4,306评论 0 0
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 11,246评论 0 4
  • IOC 控制反转容器控制程序对象之间的关系,而不是传统实现中,有程序代码之间控制,又名依赖注入。All 类的创建,...
    irckwk1阅读 4,652评论 0 0

友情链接更多精彩内容