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. windows server2012 图形加速,玩游戏不掉帧

    研究了很久,其实就是将 电源管理设置成 高级 ....

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

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

  3. stm32 IAP + APP ==>双剑合一(转)

    源:http://blog.csdn.net/yx_l128125/article/details/13591743 (扩展-IAP主要用于产品出厂后应用程序的更新作用,上一篇博文详细的对IAP 升级 ...

  4. Mysql(集群)业务水平切割 垂直切割(Amoeba)

     Amoeba原理戳这里:Amoeba详细介绍 需要根据企业 数据业务进行切割,垂直切割又称为纵向切割. 垂直切割通说的说就是有多个表,对表进行分离(用户数据.博客文章数据.照片数据.标签数据类型.群 ...

  5. The 2014 ACM-ICPC Asia Mudanjiang Regional

    继续复盘之前的Regional......出题者说这一套题太简单,对当时没有AK很不满......真是醉了,弱校没法活了 [A]签到题 [B]树结构,树的中心 [C]-_-/// [D]概率DP [E ...

  6. web项目编译出错时,原因之一,可能是build path 中order and Export引起

    build path中的order and Export,如果两个libarary中有相同功能的jar包,则编译器会选择顺序在前的jar包中相应的类作为编译所需. 所以,当项目jar包较多的时候,如果 ...

  7. Inside dependency property

    依赖属性的定义,分为3步(以PresentationFramework中的System.Windows.Controls.Button为例) 1.  声明依赖属性 public static read ...

  8. STM32音乐播放器,文件查找的实现

    使用FATFS只是完成了一个基本的文件读写,有时候我们需要扩展一些功能,比如MP3实验,需要上一曲下一曲的切换,扩展的代码如下 //显示目录下所有文件 u8 ShowFileList(u8* dirP ...

  9. 《SpringMVC数据绑定入门》笔记

    基本类型 最好使用封装类型 简单多数据&多层级对象 简单多数据 单个对象,直接使用属性名=值即可 多层级对象 属性.属性=值即可 同属性多对象 WebDataBinder只在当前类中生效,不是 ...

  10. tp框架实现验证码验证

    //实现验证页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...