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/> 中文意思:<上 ...
随机推荐
- Java设计模式--Java Builder模式
1.Java Builder模式主要是用一个内部类去实例化一个对象,避免一个类出现过多构造函数,而且构造函数如果出现默认参数的话,很容易出错. public Person(String name) P ...
- android usb挂载分析
http://blog.csdn.net/new_abc/article/details/7409018
- 设置gridcontrol的焦点行
private void gridView1_DoubleClick(object sender, EventArgs e) { try { ...
- gridcontrol第一行为0,没有选中为-999999
- -linux删除大量文件----rm,rsync
要在linux下删除海量文件,比如有数十万个文件,此时常用的rm -rf * 就会等待时间很长.这时我们可以使用rsync快速删除大量文件. 1.建立一个空目录 mkdir -p /tmp/rsync ...
- 第三方app抽奖发送微信红包
1.控制器方法: private string SendRedPackge(string OpenId, int Amount, string LuckyCode) { Models.PayWeiXi ...
- 7 -- Spring的基本用法 -- 11...
7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...
- IO之同步、异步、阻塞、非阻塞
Stevens在文章中一共比较了五种IO Model: blocking IO nonblocking IO IO multiplexing signal driven IO ...
- Jetty实战之 嵌入式Jetty运行Servlet
http://blog.csdn.net/kongxx/article/details/7230080 Jetty实战之 嵌入式Jetty运行Servlet 分类:JettyJava (19530) ...
- HTML学习(八)列表和块
无序列表无序列表是一个项目的列表,此列项目使用粗体圆点(典型的小黑圆圈)进行标记.无序列表始于 <ul> 标签.每个列表项始于 <li>.<ul type=“”> ...