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()");
}
}
Spring xml中进行面向切面的配置的更多相关文章
- spring学习总结二-----面向切面编程(AOP)思想
上一篇spring博客简总结了spring控制反转和依赖注入的相关思想知识点,这篇博文对spring的面向切的编程思想进行简单的梳理和总结. 一.面向切面的思想 与面向对象的纵向关系概念不同,面向切面 ...
- spring框架中AOP思想与各种配置详解
Spring中提供两种AOP支持: 1.基于代理的经典AOP 2.Aspectj注解配置AOP 首先我们先了解什么是AOP,AOP(Aspect Oriented Programming ...
- Java中的面向切面编程(AOP)
一.什么是AOP? Aspect Oriented Programming ,即面向切面编程. AOP是对面向对象编程的一个补充. 它的目的是将复杂的需求分解为不同的切面,将散布在系统中的公共功能集中 ...
- Spring框架中的定时器 使用和配置
Spring框架中的定时器 如何使用和配置 转载自:<Spring框架中的定时器 如何使用和配置>https://www.cnblogs.com/longqingyang/p/554543 ...
- [译]如何在ASP.NET Core中实现面向切面编程(AOP)
原文地址:ASPECT ORIENTED PROGRAMMING USING PROXIES IN ASP.NET CORE 原文作者:ZANID HAYTAM 译文地址:如何在ASP.NET Cor ...
- Spring Boot中只能有一个WebMvcConfigurationSupport配置类
首先将结论写文章的最前面,一个项目中只能有一个继承WebMvcConfigurationSupport的@Configuration类(使用@EnableMvc效果相同),如果存在多个这样的类,只有一 ...
- Spring中的面向切面编程(AOP)简介
一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...
- Spring实战(十一) 在Spring XML中配置AOP
如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了. 1.Spring XML配置文件 解析参考:http://www.cnblogs.com/bi ...
- Spring.xml中配置注解context:annotation-config和context:component-scan简述
XML中context:annotation-config和context:component-scan简述 <context:annotation-config/> 中文意思:<上 ...
随机推荐
- Android SystemProperties设置/取得系统属性的用法总结
通过调查得知,Android系统中取得/设置系统属性的用法参考以下3篇文章就足够了. 1.Android SystemProperties简介 介绍了设置属性需要的权限,已经设置权限的方法. Syst ...
- 改变MyEclipse创建JSP时默认的pageEncoding编码
如何改变MyEclipse创建JSP时默认的pageEncoding编码 有时我们需要改变MyEclipse创建JSP时默认的pageEncoding编码,因为也许它默认的编码不是我们想要的,比如我们 ...
- at 定时任务
每天一个linux命令(49):at命令 在windows系统中,windows提供了计划任务这一功能,在控制面板 -> 性能与维护 -> 任务计划, 它的功能就是安排自动运行的任务. ...
- Zabbix的安装及简单配置
Mysql源码安装:Mysql安装脚本 PHP源码安装:基于LNMP的Zabbbix之PHP源码安装 Nginx源码安装:Nginx安装 Zabbix监控端源码安装(包含Server和Agent):l ...
- 3个人一起写的EI论文可以检索到啦~ --> Exploring the use of a 3D Virtual Environment in Chinese Cultural Transmission
<a href='http://www.engineeringvillage.com/blog/document.url?mid=cpx_10ed754f14b5b7381b6M764b1017 ...
- laravel5 html引用问题
1. Composer 安装 编辑 composer.json 文件, require 节点下增加: "illuminate/html": "~5.0" 然后 ...
- iOS技术框架构和更新版本的技术特性
Core OS层 Sytem 系统层包括内核环境,驱动及操作系统层unix接口.内核以mach为基础,它 负责操作系统的各个方面,包括管理系统的虚拟内存,线程,文件系统,网络以及进程间通讯.这一层包含 ...
- MYSQL-group_concat设置group_concat_max_len
MySQL提供的group_concat函数可以拼接某个字段值成字符串,如 select group_concat(user_name) from sys_user,默认的分隔符是 逗号,即" ...
- 【repost】JS中的hook机制
hook机制也就是钩子机制,由表驱动实现,常用来处理多种特殊情况的处理.我们预定义了一些钩子,在常用的代码逻辑中去适配一些特殊的事件,这样可以让我们少些很多if else语句.举个高考加分的例子,比如 ...
- ucos信号量集源码分析
在实际的应用之中,一个任务经常需要等待多个信号量的同时生效,或者说任务需要根据多个信号量的组合作用的结果来决定任务的运行方式,为了实现这种多信号量组合的功能,ucos实现了信号量集的特殊结构. 信号量 ...