Loading

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()");
    }
}

 

posted @ 2015-09-28 16:17  stono  阅读(438)  评论(0编辑  收藏  举报