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

Spring xml中进行面向切面的配置的更多相关文章

  1. spring学习总结二-----面向切面编程(AOP)思想

    上一篇spring博客简总结了spring控制反转和依赖注入的相关思想知识点,这篇博文对spring的面向切的编程思想进行简单的梳理和总结. 一.面向切面的思想 与面向对象的纵向关系概念不同,面向切面 ...

  2. spring框架中AOP思想与各种配置详解

    Spring中提供两种AOP支持:   1.基于代理的经典AOP   2.Aspectj注解配置AOP    首先我们先了解什么是AOP,AOP(Aspect Oriented Programming ...

  3. Java中的面向切面编程(AOP)

    一.什么是AOP? Aspect Oriented Programming ,即面向切面编程. AOP是对面向对象编程的一个补充. 它的目的是将复杂的需求分解为不同的切面,将散布在系统中的公共功能集中 ...

  4. Spring框架中的定时器 使用和配置

    Spring框架中的定时器 如何使用和配置 转载自:<Spring框架中的定时器 如何使用和配置>https://www.cnblogs.com/longqingyang/p/554543 ...

  5. [译]如何在ASP.NET Core中实现面向切面编程(AOP)

    原文地址:ASPECT ORIENTED PROGRAMMING USING PROXIES IN ASP.NET CORE 原文作者:ZANID HAYTAM 译文地址:如何在ASP.NET Cor ...

  6. Spring Boot中只能有一个WebMvcConfigurationSupport配置类

    首先将结论写文章的最前面,一个项目中只能有一个继承WebMvcConfigurationSupport的@Configuration类(使用@EnableMvc效果相同),如果存在多个这样的类,只有一 ...

  7. Spring中的面向切面编程(AOP)简介

    一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...

  8. Spring实战(十一) 在Spring XML中配置AOP

    如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了. 1.Spring XML配置文件 解析参考:http://www.cnblogs.com/bi ...

  9. Spring.xml中配置注解context:annotation-config和context:component-scan简述

    XML中context:annotation-config和context:component-scan简述 <context:annotation-config/> 中文意思:<上 ...

随机推荐

  1. 请教<context:component-scan/>和<mvc:annotation-driven/>的区别20

    http://www.iteye.com/problems/66133 FileSystemXmlApplicationContext

  2. Pythagorean Triples

    Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. PAT (Advanced Level) 1019. General Palindromic Number (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  4. STL优先队列的使用

    STL中有一个优先队列的容器可以使用. [头文件] queue 队列容器 vector 向量容器 [操作] 优先级队列支持的操作 q.empty()         如果队列为空,则返回true,否则 ...

  5. HDU 3874 Necklace

    莫队算法. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #in ...

  6. (译)Windsor入门教程---第二部分 引用Windsor

    原文:http://docs.castleproject.org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx ...

  7. 18、手把手教你Extjs5(十八)模块记录的拖放删除、拖放复制新增

    网页当中的拖放(drag-drop)是比较有趣的操作,extjs5中很好的封装了拖放的动作,也有各种类来支持,但是要学好“拖放”这个东西真是很难,特别是象我这样英语不好的人,看不太懂官网上的说明,做一 ...

  8. 10、手把手教你Extjs5(十)自定义模块的设计

    从这一节开始我们来设计并完成一个自定义模块.我们先来确定一个独立的模块的所能定义的一些模块信息.以下信息只是我自己在开发过程中想到或用到的,希望有新的想法的或者有建议的跟贴回复. 一个独立模块包含以下 ...

  9. iOS开发——绘图电池按百分比显示

    1.在DrawLine.h文件中提供了一个改变电池电量数值的接口 // //  DrawLine.h //  Demo-draw2 // //  Created by yyt on 16/5/11. ...

  10. WIFI机器人网

    WIFI机器人网 WIFI智能小车机器人 外网远程控制WIFI智能小车机器人(WIFI板/703N)