Spring XML配置实现AOP
1: 首先我们要定义 配置成切面的类
package cn.gbx.example; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; public class MyXmlIntercept { public void doAccessCheck() { System.out.println("前置通知");
} public void doAfterReturning() {
System.out.println("后置通知");
} public void doAfter() {
System.out.println("最终通知");
} public void doAfterThrowing() {
System.out.println("例外通知");
} public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入方法");
Object value = pjp.proceed();
System.out.println("退出方法");
return value;
}
}
然后再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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="personService" class="cn.gbx.serviceimpl.PersonServiceImpl"></bean> <bean id="myXmlIntercept" class="cn.gbx.example.MyXmlIntercept"></bean>
<aop:config>
<aop:aspect id="myasp" ref="myXmlIntercept">
<aop:pointcut expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))" id="mycut"/>
<aop:before method="doAccessCheck" pointcut-ref="mycut"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
</beans>
然后测试即可。
简单说明
expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))"
如果我们要要返回值烈性为Stirng的 方法才被拦截
execution (java.lang.String cn.gbx.serviceimpl.PersonServiceImpl.*(..)) 如果我们要求参数第一个为String , 后边不限
expression="execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(java.lang.String, ..))" 要求返回至为非void
expression="execution (!void cn.gbx.serviceimpl.PersonServiceImpl.*(..))"
Spring XML配置实现AOP的更多相关文章
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- 使用Spring框架入门三:基于XML配置的AOP的使用
一.引入Jar包 <!--测试1使用--> <dependency> <groupId>org.springframework</groupId> &l ...
- 关于xml配置实现AOP的小知识
除了前面介绍的基于JDK1.5的注解方式来定义切面,切入点和增强处理外,Spring AOP也允许直接使用XML配置文件来管理它们.在JDK1.5之前,只能使用配置文件的方式来管理,在Spring2. ...
- 2015年12月10日 spring初级知识讲解(二)最小化Spring XML配置 注解
序,随着Spring容器管理Bean数量增加,XML文件会越来越大,而且纯手工配置XML很繁琐,Spring和JAVA都提供了一些注解方式用以简化XML配置. 目录 一.自动装配(autowiring ...
- Java基础---Java---基础加强---类加载器、委托机制、AOP、 动态代理技术、让动态生成的类成为目标类的代理、实现Spring可配置的AOP框架
类加载器 Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader,AppClassLoader 类加载器也是Jav ...
- spring xml配置注入改为手动注入过程
项目中需要使用MQ组件来接受消息,但是有的时候,在使用的时候,并不能满足spring注入的条件,无法注入.例如 在jfinal的config的afterJFinalStart中,由于jfinal集成s ...
- 最小化Spring XML配置
Spring提供两种技巧,可以帮助我们减少XML的配置数量. 1.自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg&g ...
- Spring XML配置里的Bean自动装配
Spring自动装配 这段是我们之前编写的代码,代码中我们使用了P命名空间 并且使用手动装配的方式将car <bean id="address" class="cn ...
- 基于XML配置的AOP实现日志打印
Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...
随机推荐
- halcon学习笔记——机器视觉工程应用的开发思路【转】
转自:http://www.cnblogs.com/hanzhaoxin/archive/2013/02/15/2912879.html 机器视觉工程应用主要可划分为硬件和软件两大部分. 硬件:工程应 ...
- flex mx组件和s组件的字体兼容性不一致
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- Mootools插件-闪烁的标题
转自:http://www.cnblogs.com/see7di/archive/2012/10/09/2716024.html 回想起来,我已经好久没有写点啥了,尤其是关于Mootools方面的东西 ...
- vscode使用php调试
1:首先查看是否安装xdebug扩展 打开终端 ➜ ~ php -vPHP 5.6.24 (cli) (built: Jul 21 2016 14:27:54) Copyright (c) 1997 ...
- Serializable接口使用纪实
这两天依领导要求使用sonar工具测试了一下项目代码,其中有一个问题是 而这个类的结构大概是这样的: public class Demo<T> implements Serializabl ...
- [转]Mac OS X framework 解析
转载地址:http://hi.baidu.com/yonderbyron/item/9838b73472152e009cc65ec8 Mac OS X framework 解析 1.framework ...
- 对table的tr使用display:block显示colspan失效问题的解决
qqqq <table> <tr> <td id="qqq" colspan="3" style="display:no ...
- HDU 4810 Wall Painting
Wall Painting Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- reactjs入门到实战(七)---- React的组件的生命周期
React的组件的生命周期有三个状态分别是:挂载(生产组件示例化.准备挂载到页面.挂载到页面).更新(更新值.更新DOM).和卸载(卸载后). >>>其他 getInitia ...
- PHP中的XML解析的5种方法
[前言]不管是桌面软件开发,还是WEB应用,XML无处不在!然而在平时的工作中,仅仅是使用一些已经封装好的类对XML对于处理,包括生成,解析等.假期有空,于是将PHP中的几种XML解析方法总结如下: ...