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 ...
随机推荐
- android 之 Crash信息的持久化处理
需求: 持久化运行时异常的信息 1.CrashHandler.java import android.content.Context; import android.content.pm.Packag ...
- error:could not open D:\java\jre1.8\lib\i386\jvm.cfg
复制一份jre到eclipse的目录下就可以了.
- thinkPHP3.2.3完整版 在sae上面的部署
第一步: thinkPHP3.2.3完整版,目录结构如下 第二步:在新浪sae上面创建一个新应用 第三步:用svn down,下来会有两个文件:index.php.config.ya ...
- symfony中twig的模板变量与注释
程序会传递给模板若干变量,你需要在模板里输出他们.例如输出$hello .1{{ hello }}.如果传递给模板的是对象或者数组,你可以使用点. 来输出对象的属性或者方法,或者数组的成员.或者你可以 ...
- js的动态加载、缓存、更新以及复用
使用范围: OA.MIS.ERP等信息管理类的项目,暂时不考虑网站. 遇到的问题: 完成一个项目,往往需要引用很多js文件,比如jQuery.js.easyUI等.还有自己写的一些列js文件,那么这些 ...
- 《深度探索C++对象模型》1
C++对象模型: 多重继承模型示意: 第二章:构造函数 语意学 基类和派生类: Bear yogi; ZooAnimal franny=yogi; 在这里,很容易理解合成的copy构造函数将vptr指 ...
- 杭电1005-Number Sequence
问题描述 A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2) ...
- easyrtc-server在ubuntu14.04上的安装方法
easyrtc 官网 http://easyrtc.com/ 1.安装nodejs,安装npm (不知道如何安装请google一下) 2. 查看运行easyrtc 所需要的js 包,在easyrtc ...
- 算法心得2:关于k个最小和问题的思考
问题描述如下: 有k个整数数组,各包含k个元素.在每个数组中取一个元素加起来,可以得到k^k个和.求这些和中最小的k个值(重复计算的算多次). 如果同时考虑这k个数组的取值情况,其复杂程度不言而喻,并 ...
- 封装NPOI导出含下拉列表的Excel
/// <summary> /// 绑定数据的方法 /// </summary> /// <param name="ssfworkbook">H ...