zoukankan      html  css  js  c++  java
  • Spring xml中进行面向切面的配置

    Spring xml中进行面向切面的配置

    XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
        <context:component-scan base-package="com.stono.sprtest2"></context:component-scan>
        <!-- 最好在xml里面配置切面Bean,这样可以有method的提示;用@Component程序一样可以运行,但是写XML没有method提示 -->
        <bean id="audience" class="com.stono.sprtest2.Audience"></bean>
        <aop:config>
            <aop:aspect ref="audience">
                <!-- execution(*-返回值 方法全名 (..)任意参数)中的意义 -->
                <aop:before pointcut="execution( * com.stono.sprtest2.Singer.perform(..))" method="takeSeat" />
                <aop:before pointcut="execution( * com.stono.sprtest2.Singer.perform(..))" method="turnOffPhone" />
                <aop:after-returning pointcut="execution( * *.perform(..))" method="applaud" />
                <aop:after-returning pointcut="execution( * *(..))" method="applaud" />
                <aop:after-returning pointcut="execution( * *.*(..))" method="applaud" />
                <!-- 这样写不行 <aop:after-returning pointcut="execution( * *.*.perform(..))" method="applaud" /> -->
                <aop:after-throwing pointcut="execution( * com.stono.sprtest2.Singer.perform(..))" method="refund" />
            </aop:aspect>
        </aop:config>
        <aop:config>
            <aop:aspect ref="audience">
                <!-- 可以把切点定义提取出来 -->
                <aop:pointcut expression="execution( * com.stono.sprtest2.Singer.perform(..))" id="performance" />
                <aop:before method="takeSeat" pointcut-ref="performance" />
                <aop:before method="turnOffPhone" pointcut-ref="performance" />
                <aop:after-returning method="applaud" pointcut-ref="performance" />
                <aop:after-throwing method="refund" pointcut-ref="performance" />
            </aop:aspect>
        </aop:config>
        <aop:config>
            <aop:aspect ref="audience">
                <!-- 可以把切点定义提取出来 -->
                <aop:pointcut expression="execution( * com.stono.sprtest2.Singer.perform(..))" id="performance" />
                <!-- 环绕aop -->
                <aop:around method="watchPerformance" pointcut-ref="performance" />
            </aop:aspect>
        </aop:config>
    </beans>

    AppBean:

    package com.stono.sprtest2;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AppBeans9 {
        @SuppressWarnings("resource")
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("appbeans9.xml");
            Singer singer = (Singer) context.getBean("singer");
            singer.perform();
        }
    }

    切面:

    package com.stono.sprtest2;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class Audience {
        public void takeSeat() {
            System.out.println("com.stono.sprtest2.Audience.takeSeat()");
        }
        public void turnOffPhone() {
            System.out.println("com.stono.sprtest2.Audience.turnOffPhone()");
        }
        public void applaud() {
            System.out.println("com.stono.sprtest2.Audience.applaud()");
        }
        public void refund() {
            System.out.println("com.stono.sprtest2.Audience.refund()");
        }
        public void watchPerformance(ProceedingJoinPoint joinPoint) {
            try {
                System.out.println("The audience is taking their seats.");
                System.out.println("The audience is turning off their cellphones.");
                long start = System.currentTimeMillis();
                joinPoint.proceed();
                long end = System.currentTimeMillis();
                System.out.println("CLAP CLAP CLAP CLAP CLAP CLAP");
                System.out.println("The performance took " + (end - start) + " milliseconds.");
            } catch (Throwable e) {
                e.printStackTrace();
                System.out.println("Boo! We want our money back!");
            }
        }
    }

    POJO:

    package com.stono.sprtest2;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Singer {
        public void perform(){
            System.out.println("com.stono.sprtest2.Singer.Perform()");
        }
    }
  • 相关阅读:
    替换gitlab自带的Nginx,并修改仓库存储路径
    linux 内网scp 无密码传输
    centos7 安装docker及Hyperf
    VMware 安装centos 7 及自动挂载共享文件夹
    基于 Thrift + Laravel RPC 调用实现
    PHP计算两个经纬度地点之间的距离
    sql server 2008安装过程中服务器配置出错
    SQL Server2008如何设置开启远程连接
    向上下左右不间断无缝滚动图片的效果(兼容火狐和IE)
    彻底解决Google浏览器CSS居中问题
  • 原文地址:https://www.cnblogs.com/stono/p/4844288.html
Copyright © 2011-2022 走看看