spring AOP 配置
2016-03-25 16:29
513 查看
需要的JAR包:antlr-2.7.6.jar aspectjrt.jar aspectjweaver.jar cglib-nodep-2.1_3.jar
首先创建一个普通类(主要类)
写一个切面类
spring中两种方式配置AOP 一种是annotation,另一种是xml
annotion方式的配置
在spring的配置文件中加入:
首先创建一个普通类(主要类)
<span style="font-size:14px;">public class aopServer { public void sayhello(){ System.out.println("aopServer"); } }</span>
写一个切面类
<span style="font-size:14px;">public class aopLog { public void berfor(){ System.out.println("berfor log"); } }</span>
spring中两种方式配置AOP 一种是annotation,另一种是xml
annotion方式的配置
在spring的配置文件中加入:
<span style="white-space:pre"> </span><!-- 启用注解方式配置AOP --> <context:annotation-config/> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 扫描包--> <context:component-scan base-package="com.log"></context:component-scan>
在切面类上添加注解 @Aspect //定义为切面类 @Component //注入IOC容器 在切面类的对应方法上添加注解@Berfore或@after ,这两个的value值为execution表达式 :execution(public void com.server.aopServer.sayhello()) 可以“*” 匹配,“..” 表示方法的任意参数 XML方式配置:
<span style="font-family: Arial, Helvetica, sans-serif;"><aop:config></span>
<aop:aspect id="myAop" ref="log"> <aop:pointcut id="target" expression="execution(* com.server.aopServer.sayhello(..))"/> <aop:before method="berfor" pointcut-ref="target"/> </aop:aspect> </aop:config>
相关文章推荐
- java平台的常用资源
- java 集合框架
- MyEclipse Trial Expired,手动注册MyEclipse
- static使用小结
- JAVA 实现多线程
- spring开发中如果service类里少了“@Transactional”,会造成dao类的数据库操作失败
- Kafka Consumer java api 配置
- eclipse边框以及代码区域黑色设置
- maven搭建spring mvc+spring jdbc项目
- 生成n位格雷码
- springMvc的Hello world(xml配置)
- Java反转单链表(code)
- java 泛型小总结
- struts2.properties
- Spring和Mybatis整合时无法读取properties的处理方案
- 做一个合格的程序猿之浅析Spring AOP源码(十七) Spring AOP开发大作战
- Rxjava原理探索:切换线程,变换
- java中的url 编码与解码
- eclipse转Android studio遇到的那些坑
- Java 线程池