spring aop
2015-12-25 11:06
645 查看
一、Aop central concepts
Aspect(横切面):一个横切多个类的模块,在企业级java应用中,事务管理就是一个很好的横切面的例子,在spring
Aop模块中,aspect有两种实现方式,一是xml配置,二是注解
Join point:将要被Point
Cut用来收集的函数(普通的函数)
Advice:当满足Pointcut时,要执行的操作(around,before,after)
Pointcut:用来收集Join
Point,对满足条件的join point进行拦截
Target object:要进行横切的对象
Aop proxy:代理
二、Types of advice(adive有5中类型)
Before advice 方法执行之前被调用
After returning advice 方法执行成功以后调用
After throwing advice 当调用异常的时候调用
After (finally) advice 方法执行以后调用,不过有没有调用成功
Around advice 方法执行前和后调用
三、空的xml-Based文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!--定义aop:config-->
<!--定义bean-->
</beans>
四、Aop配置示例一
<aop:config>
<aop:aspect
id="advice" ref="adviceImp">
<aop:before
pointcut="execution(* com.sunny.test.*.*(..))" method="doBefore"/>
</aop:aspect>
</aop:config>
<!-- Definition
aspect -->
<bean id="adviceImp"
class="com.sunny.spring_aop.AdviceImp"/>
<!-- Definition
Bean -->
<bean id="test"
class="com.sunny.test.Test"/>
说明:
adviceImp为Aspect的实现即为Advice
<aop:config>为Aop的配置实例,<aop:before>在函数之前调用
pointcut="execution(* com.sunny.test.*.*(..))"收集join point,然后执行method中配置的函数
<bean id="test" class="com.sunny.test.Test"/> 这个bean中的函数将会pointcut收集,注意这个Test类的示例只能在这里生产(用getBean来产生示例),如果用new
Test()来产生实例将不会被piontcut收集
完成上述配置,调用Test类中的方法时候,Advice中的doBefore方法将会在该函数之前执行
execution(* com.sunny.test.Test.*(..))对com.sunny.test包下面的Test类进行pointcut操作
execution(* com.sunny.test.Test.fun(..))对com.sunny.test.Test类下面的fun方法进行pointcut操作
execution(* com.sunny.test.Test.fun(String))对com.sunny.test.Test类下面的fun(String)方法进行pointcut操作
上述的aop:config可用下配置(把pointcut放在一个全局配置的位置)
<aop:config>
<aop:aspect
id="advice" ref="adviceImp">
<aop:pointcut
id="pointc" expression="execution(* com.sunny.test.*.*(..))"/>
<aop:before
pointcut-ref="pointc" method="doBefore"/>
</aop:aspect>
</aop:config>
参考官网文档:http://docs.spring.io/spring/docs/4.1.8.RELEASE/spring-framework-reference/htmlsingle/
Aspect(横切面):一个横切多个类的模块,在企业级java应用中,事务管理就是一个很好的横切面的例子,在spring
Aop模块中,aspect有两种实现方式,一是xml配置,二是注解
Join point:将要被Point
Cut用来收集的函数(普通的函数)
Advice:当满足Pointcut时,要执行的操作(around,before,after)
Pointcut:用来收集Join
Point,对满足条件的join point进行拦截
Target object:要进行横切的对象
Aop proxy:代理
二、Types of advice(adive有5中类型)
Before advice 方法执行之前被调用
After returning advice 方法执行成功以后调用
After throwing advice 当调用异常的时候调用
After (finally) advice 方法执行以后调用,不过有没有调用成功
Around advice 方法执行前和后调用
三、空的xml-Based文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!--定义aop:config-->
<!--定义bean-->
</beans>
四、Aop配置示例一
<aop:config>
<aop:aspect
id="advice" ref="adviceImp">
<aop:before
pointcut="execution(* com.sunny.test.*.*(..))" method="doBefore"/>
</aop:aspect>
</aop:config>
<!-- Definition
aspect -->
<bean id="adviceImp"
class="com.sunny.spring_aop.AdviceImp"/>
<!-- Definition
Bean -->
<bean id="test"
class="com.sunny.test.Test"/>
说明:
adviceImp为Aspect的实现即为Advice
<aop:config>为Aop的配置实例,<aop:before>在函数之前调用
pointcut="execution(* com.sunny.test.*.*(..))"收集join point,然后执行method中配置的函数
<bean id="test" class="com.sunny.test.Test"/> 这个bean中的函数将会pointcut收集,注意这个Test类的示例只能在这里生产(用getBean来产生示例),如果用new
Test()来产生实例将不会被piontcut收集
完成上述配置,调用Test类中的方法时候,Advice中的doBefore方法将会在该函数之前执行
execution(* com.sunny.test.Test.*(..))对com.sunny.test包下面的Test类进行pointcut操作
execution(* com.sunny.test.Test.fun(..))对com.sunny.test.Test类下面的fun方法进行pointcut操作
execution(* com.sunny.test.Test.fun(String))对com.sunny.test.Test类下面的fun(String)方法进行pointcut操作
上述的aop:config可用下配置(把pointcut放在一个全局配置的位置)
<aop:config>
<aop:aspect
id="advice" ref="adviceImp">
<aop:pointcut
id="pointc" expression="execution(* com.sunny.test.*.*(..))"/>
<aop:before
pointcut-ref="pointc" method="doBefore"/>
</aop:aspect>
</aop:config>
参考官网文档:http://docs.spring.io/spring/docs/4.1.8.RELEASE/spring-framework-reference/htmlsingle/
相关文章推荐
- Spring Framework 4.2 源码部署到eclipse
- Spring整合JMS(一)——基于ActiveMQ实现
- Java面向对象编程-第11章学习笔记
- java.exe和javaw.exe的区别
- hibernate主键生成策略
- Eclipse 开启卡死在 Android SDK Content Loader初始化加载
- Spring国际化动态经典配置
- javamail API
- Java,MD5
- MyEclipse2014安装及使用svn插件
- Java关键字
- JAVA爬虫WebCollector 2.x入门教程——基本概念
- Java设计模式_行为型_访问者模式_每个人的不同结果
- Java Web系列:Spring依赖注入基础
- 关于Spring+quartz定时任务时触发2次的问题
- [笔记] JPush快速集成_Eclipse
- Java 多线程(一) 基础知识与概念
- Spring动态拦截器
- java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle,
- eclipse 中全局替换