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/> 中文意思:<上 ...
随机推荐
- SVN MERGE 和冲突
摘要:最佳做法是避免冲突.冲突时,不要把branch merge到trunk. 先由最新版本的trunk得到branch,然后再修改文件,直接merge过去就行.这样不会有冲突.先用svn merge ...
- Java谜题——类谜题(二)
1.域的隐藏 代码如下: class Base { public String className = "Base"; } class Derived extends Base { ...
- hadoop第一篇
1 hadoop整体架构 2 各组件关系 hdfs只是一个存储空间,他的完整名字是分布式文件系统.有名可知他的作用了. hbase是一个内存数据库,简单点说hbase把表啊什么的存在hdfs上.
- html5绘图
html5绘图 这是我在绘图过程中遇到的问题,求助高手帮忙啊... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- STM32-NVIC中断管理实现[直接操作寄存器]
源:stm32 NVIC中断管理实现[直接操作寄存器] cortex-m3支持256个中端,其中包含了16个内核中断,240个外部中断.stm32只有84个中断,包括16个内核中断和68个可屏 ...
- android 点滴记录
1.AndroidM环境下,在framework层添加代码会对jar包的package name进行检查,并提示”unknown package name of class file”怎么解决? 产生 ...
- iOS开发——MD5加密
#import <CommonCrypto/CommonDigest.h> - (NSString *)md5:(NSString *)str { const char *cStr = [ ...
- 企业证书APP发布流程 分类: ios相关 app相关 2015-06-10 11:01 212人阅读 评论(0) 收藏
企业发布app的 过程比app store 发布的简单多了,没那么多的要求,哈 但是整个工程的要求还是一样,比如各种像素的icon啊 命名规范啊等等. 下面是具体的流程 1.修改你的 bundle i ...
- Mysql的收获
今天公司的业务需求中,遇到一个很郁闷的情况. 有一个申请记录,缺少申请原因的字段. 我以为很简单,采用left jion 很容易获取到申请原因. SELECT a.*,b.RealName,c.Dep ...