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()");
        }
    }
  • 相关阅读:
    delphi 实体类 JSON 数组 TJsonSerializer Deserialize
    IIS 禁止访问:在 Web 服务器上已拒绝目录列表
    ASP.NET 一般处理程序
    .net 架构
    delphi XE8 NetHTTPRequest NetHTTPClient
    ASP.NET web 应用程序项目
    HttpClient
    eclipse Android 开发基础 Activity 窗体 界面
    关闭 iTunes 自动同步
    could not be installed at this time
  • 原文地址:https://www.cnblogs.com/stono/p/4844288.html
Copyright © 2011-2022 走看看