Spring 4.0 学习日记(9) ---XML配置实现AOP切面
来源:互联网 发布:java随机数生成 编辑:程序博客网 时间:2024/06/07 06:42
Spring创建代理的规则
1.默认使用Java动态代理来创建AOP代理
2.当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理,也可强制使用CGLIB
其实Xml配置更简单
直接看代码就懂了
接口类
package com.wow.AopMessageInstance;public interface HelloWorld { void printHelloWorld(); void doPrint(); String getReturn();}
实现类1
package com.wow.AopMessageInstance;public class HelloWorldImpl implements HelloWorld { @Override public void printHelloWorld() { System.out.println("Enter HelloWorldImpl.printHelloWorld()"); } @Override public void doPrint() { System.out.println("Enter HelloWorldImpl.doPrint()"); } @Override public String getReturn() { String str = "HelloWorldImpl" ; System.out.println("Enter HelloWorldImpl.getReturn()"); return str; }}
实现类2
package com.wow.AopMessageInstance;public class HelloWorldImplAnother implements HelloWorld { @Override public void printHelloWorld() { System.out.println("Enter HelloWorldImplAnother.printHelloWorld()"); } @Override public void doPrint() { System.out.println("Enter HelloWorldImplAnother.doPrint()"); } @Override public String getReturn() { String str = "HelloWorldImpl" ; System.out.println("Enter HelloWorldImplAnother.getReturn()"); return str; }}
Aop切面
package com.wow.AopMessageInstance;public class HelloWorldAop { public void printTime() { System.out.println("CurrentTime = " + System.currentTimeMillis()); } public void getReturn(String obj) { System.out.println("ReturnValue = " + obj); }}
测试类
package com.wow.AopMessageInstance;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWorldTest { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml"); HelloWorld hw = (HelloWorld) app.getBean("helloWorldImpl"); HelloWorld hwa = (HelloWorld) app.getBean("helloWorldImplAnother"); hw.printHelloWorld(); hw.doPrint(); hw.getReturn(); System.out.println("----"); hwa.printHelloWorld(); hwa.doPrint(); hwa.getReturn(); }}
beans.xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id = "helloWorldImpl" class = "com.wow.AopMessageInstance.HelloWorldImpl"></bean> <bean id = "helloWorldImplAnother" class = "com.wow.AopMessageInstance.HelloWorldImplAnother"></bean> <bean id = "helloWorldAop" class = "com.wow.AopMessageInstance.HelloWorldAop"></bean> <!-- 如果只想织入接口中的某些方法 只用修改expresion的匹配方式就好了--> <aop:config> <aop:aspect id = "aop" ref = "helloWorldAop"> <aop:pointcut id="pointCut" expression="execution(* com.wow.AopMessageInstance.*.*(..))" /> <aop:before method="printTime" pointcut-ref="pointCut" /> <aop:after method="printTime" pointcut-ref="pointCut" /> <aop:after-returning method="getReturn" pointcut-ref="pointCut" returning="obj"/> </aop:aspect> <!-- 如果需要有多个切面 只要在这里再写一个aop:aspect属性就好了 对于多个切面的前后顺序 可以用到order属性 --> <!-- <aop:aspect id="time" ref="timeHandler" order="1"> <aop:pointcut id="addTime" expression="execution(* com.xrq.aop.HelloWorld.print*(..))" /> <aop:before method="printTime" pointcut-ref="addTime" /> <aop:after method="printTime" pointcut-ref="addTime" /> </aop:aspect> <aop:aspect id="log" ref="logHandler" order="2"> <aop:pointcut id="printLog" expression="execution(* com.xrq.aop.HelloWorld.do*(..))" /> <aop:before method="LogBefore" pointcut-ref="printLog" /> <aop:after method="LogAfter" pointcut-ref="printLog" /> </aop:aspect> --> </aop:config></beans>
打印信息
八月 02, 2017 11:32:25 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@142b7711: startup date [Wed Aug 02 23:32:25 CST 2017]; root of context hierarchy八月 02, 2017 11:32:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [beans.xml]CurrentTime = 1501687946276Enter HelloWorldImpl.printHelloWorld()CurrentTime = 1501687946276CurrentTime = 1501687946277Enter HelloWorldImpl.doPrint()CurrentTime = 1501687946277CurrentTime = 1501687946277Enter HelloWorldImpl.getReturn()CurrentTime = 1501687946277ReturnValue = HelloWorldImpl----CurrentTime = 1501687946277Enter HelloWorldImplAnother.printHelloWorld()CurrentTime = 1501687946277CurrentTime = 1501687946278Enter HelloWorldImplAnother.doPrint()CurrentTime = 1501687946278CurrentTime = 1501687946278CurrentTime = 1501687946278ReturnValue = HelloWorldImplAnother
阅读全文
0 0
- Spring 4.0 学习日记(9) ---XML配置实现AOP切面
- Spring AOP学习笔记(4):AOP切面优先级&切面表达式重用&xml配置方式
- Spring 4.0 学习日记(8) ---AOP切面注解实现五种通知
- Spring框架-AOP细节2(XML配置AOP切面)
- spring aop切面动态代理xml配置实现
- spring添加AOP切面-基于XML配置
- Spring aop切面实际应用 XML配置
- Spring aop切面实际应用 XML配置
- Spring-通过xml配置文件实现切面(AOP)
- Spring Aop切面基于Xml的学习
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- spring framework面向切面编程示例(xml配置):spring-aop
- Spring学习(九)-AOP切面通知
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- Spring学习:AOP配置1(xml)
- 谜题4:It's Elementary 初级问题
- wifi扫描工具
- 关于ehcache二级缓存
- poj2411 Mondriaan's Dream(状压dp)
- redis的 悲观锁和乐观锁的区别
- Spring 4.0 学习日记(9) ---XML配置实现AOP切面
- Excel在统计分析中的应用—第二章—描述性统计-Part4-偏度(四分位数和Spearman偏度系数)
- CSS
- 实例2.1 两个有序链表序列的合并
- Libevent源码剖析——事件event
- 2017 Multi-University Training Contest
- Linux命令学习:统计socket各种状态的连接数(sed/awk)
- 数据结构之栈之括号匹配问题
- Go语言学习笔记 --- 指针和自增自减运算及相关注意事项