zoukankan      html  css  js  c++  java
  • SpringInAction--XML配置Spring Aop

    前面学习了如何用注解的方式去配置Spring aop,今天把XML配置的方法也看了下,下面顺便也做了个记录

    先把spring中用xml配置aop的配置元素给贴出来:

    • <aop:advisor> 定义AOP通知器
    • <aop:after> 定义AOP后置通知(不管被通知的方法是否执行成功)
    • <aop:after-returning> 定义AOP返回通知
    • <aop:after-throwing> 定义AOP异常通知
    • <aop:around> 定义AOP环绕通知
    • <aop:aspect> 定义一个切面
    • <aop:aspectj-autoproxy> 启用 @AspectJ 注解驱动的切面
    • <aop:before> 定义一个AOP前置通知
    • <aop:config> 顶层的AOP配置元素。大多数的 <aop:*> 元素必须包含在 <aop:config> 元素内
    • <aop:declare-parents> 以透明的方式为被通知的对象引入额外的接口
    • <aop:pointcut> 定义一个切点

    还是先创一个接口类:

    public interface Song {
        void song();
    }

    接口实现类:

    public class ManMan implements Song {
    
        public void song() {
            System.out.println("下面这首歌是张学友的《慢慢》");
        }
    }

    下面就是需需要定义的一个类,写通知的时候需要调用的方法

    public class VocalConcert {
    
    
        public void checking() {
            System.out.println("检票之后,找位子坐下");
        }
    
        public void beautiful() {
            System.out.println("演唱会进入精彩部分的时候,鼓掌!");
        }
    
        public void leave() {
            System.out.println("结束后,我们离开场地");
        }
    
    
        public void watchVocalConcert(ProceedingJoinPoint joinPoint) {
            try {
                System.out.println("检票之后,找位子坐下");
                joinPoint.proceed();
                System.out.println("演唱会进入精彩部分的时候,鼓掌!");
                System.out.println("结束后,我们离开场地");
            } catch (Throwable throwable) {
                System.out.println("效果不好,退票");
            }
    
        }
    }

    好了,上面有已经有一个实现类,我们要让他可以实现自动注入,同时通知的类也要注入

    然后就要配置 配置的切片什么时候通知消息了:

    <?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="vocalConcert" class="com.aop.test2.VocalConcert" /> <bean class="com.aop.test2.ManMan" id="manMan"/> <!--一般的前后通知消息 --> <aop:config> <aop:aspect ref="vocalConcert"> <!-- vocalConcert 实现类调用之前,实现的方法 --> <aop:before pointcut="execution(* com.aop.test2.Song.song(..))" method="checking"/> <!-- vocalConcert 实现类调用之后,实现的方法 --> <aop:after pointcut="execution(* com.aop.test2.Song.song(..))" method="beautiful"/> <!-- vocalConcert 实现类调用结束,返回的时候,实现的方法 --> <aop:after-returning pointcut="execution(* com.aop.test2.Song.song(..))" method="leave"/> <!-- 同一个切片,定一个id 调用的方法--> <!-- <aop:pointcut id="song" expression="execution(* com.aop.test2.Song.song(..))"/> <aop:before pointcut-ref="song" method="checking"/> <aop:after pointcut-ref="song" method="beautiful"/> <aop:after-returning pointcut-ref="song" method="leave"/>--> </aop:aspect> </aop:config> <!--环绕通知 --> <aop:config> <aop:aspect ref="vocalConcert"> <aop:pointcut id="song" expression="execution(* com.aop.test2.Song.song(..))"/> <aop:around method="watchVocalConcert" pointcut-ref="song"/> </aop:aspect> </aop:config> </beans>

    测试类

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("config.xml")
    public class VocalConcertTest {
    
    
        @Autowired
        Song mm;
    
        @Test
        public void log() {
            mm.song();
        }
    
    }

    这就是简单的XML配置spring aop的方法,当然还有,还有带参数的xml配置,其主要定义的切点的时候不同,假设song()的方法是要传入参数的,参数类型为String,那么配置应该如下:

     <aop:config>
            <aop:aspect ref="vocalConcert">
                <aop:pointcut id="song" expression="execution(* com.aop.test2.Song.song(String) )and args(song)" />
            </aop:aspect>
        </aop:config>

    主要就是 expression="execution(* com.aop.test2.Song.song(String) )and args(song)" 这边需要更改,跟前面用注解配置没有多大的区别

    当然这个时候所对应的bean也要带参数,这个前面就学过,就不记录了。

     代码:https://github.com/eoooxy/springinaction test下 的com.aop.test2 中

  • 相关阅读:
    Date类型 方法
    迭代方法和归并函数
    js快速排序方法
    reset
    水平垂直居中
    css清除浮动
    box-shadow
    display---我的第一篇博客
    centos7基础安装
    aws和ufile挂载数据盘EBS
  • 原文地址:https://www.cnblogs.com/eoooxy/p/6476845.html
Copyright © 2011-2022 走看看