`
zxmsdyz
  • 浏览: 126879 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Spring AOP使用配置介绍(五):基于Schema配置的aop

阅读更多
如果项目中不能使用JDK5.0,那就无法使用基于@AspectJ注解的切面。但是我们仍可以使用AspectJ切点表达式,可以用Schema配置的方法来代替。

首先定义一个增强:
package com.maxjay.bank.advice.schema;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;

import com.maxjay.bank.model.TSysUser;

public class LoggerAdvice {
	private Logger logger = Logger.getLogger(LoggerAdvice.class);

	/**
	 * JoinPoint为连接点的信息,可以通过jp获取到代理类的类型、要执行的方法及其参数
	 * @param jp
	 */
	public void preLogger(JoinPoint jp) {
		logger.info("前置增强,使用schema方法配置");
		StringBuffer sb = new StringBuffer();
		for(Object s:jp.getArgs()){
			sb.append(s.toString()+",");
		}
		logger.info("代理对象执行的方法名为:" + jp.getSignature().getName() + ",方法参数为:"
				+ sb.toString());
	}
	
	/**
	 * JoinPoint为连接点的信息,可以通过jp获取到代理类的类型、要执行的方法及其参数
	 * @param jp
          * @param user 代理方法的返回值
	 */
	public void postLogger(JoinPoint jp, TSysUser user) {
		logger.info("后置增强,代理对象执行的结果返回值为:" + user.getUserName());
	}
}


注意该增强中没有注解也没有继承任何的接口,只是一个普通的类。该如何让spring辨别出它是一个增强呢?这就需要spring配置了,代码如下:
<?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" xmlns:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     此段头部声明必须添加,尤其是aop的声明部分,否则下面的配置无法使用
       .........
	<!-- 使用schema方式配置aop -->
	<!-- 将要使用的增强类定义成bean -->
	<bean id="loggerAdvice" class="com.maxjay.bank.advice.schema.LoggerAdvice" />
	
	<aop:config proxy-target-class="true">
		<aop:aspect ref="loggerAdvice">
			<aop:before method="preLogger"
				pointcut="target(com.maxjay.bank.service.impl.TestAdviceService) and execution(* validateUser(..))" />
			<aop:after-returning method="postLogger"
				pointcut="target(com.maxjay.bank.service.impl.TestAdviceService) and execution(* validateUser(..))" returning="user"/>
		</aop:aspect>
	</aop:config>

     <aop:config>中的proxy-target-class可以决定使用哪种代理技术,设置为true表示使用cglib代理,false表示使用jdk动态代理,<aop:config>可以定义多个,不同的<aop:config>可以采用不同的代理技术。
      <aop:aspect>中可以定义多个增强,上面的例子中<aop:before>定义的是前置增强,<aop:after-returning>定义的是后置增强。还可以用<aop:around>定义环绕增强,<aop:after-throwing>定义异常抛出异常,<aop:after>定义final增强(相当于后置增强和异常抛出增强的联合体),<aop:declare-parents>定义引介增强。
      <aop:after-returning>中的method指定了增强类中的增强方法;pointcut用来指定切面,采用的是AspectJ表达式;returning指定的是返回值在增强方法中的参数名,与上一篇中@AspectJ注解增强相似。

使用schema还可以用旧方法定义的增强:
	<!-- 使用文章(一)中用接口方法定义的增强 -->
	<bean id="loggerBeforeAdvice" class="com.maxjay.bank.advice.LoggerBeforeAdvice" />
	<aop:config proxy-target-class="true">
		<aop:advisor advice-ref="loggerBeforeAdvice"
			pointcut="target(com.maxjay.bank.service.impl.TestAdviceService) and execution(* validateUser(..))" />
	</aop:config>
分享到:
评论

相关推荐

    Spring AOP之基于Schema配置总结与案例

    Spring AOP之基于Schema配置总结与案例 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45242505

    Spring AOP源码深度解析:掌握Java高级编程核心技术

    Spring AOP(面向切面编程)是Java高级编程中的重要...Spring AOP的配置方式多样,包括基于接口的配置、schema-based配置和@AspectJ注解配置。通过这些配置方式,开发者可以灵活地实现AOP功能,满足不同场景下的需求。

    Spring AOP配置源码

    此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的关于单元测试的支持 在类上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载...

    springAOP demo 带错误解决文档

    Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop] Offending resource: class path resource [beans.xml] at org....

    Spring aop 基于schema的AOP支持及JoinPoint的使用、如何使用CGLIB代理

    NULL 博文链接:https://quicker.iteye.com/blog/673889

    11spring4_aop3.rar

    第三种实现方法—通过注解来实现 签名 注解实现aop &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=... &lt;aop:aspectj-autoproxy/&gt; &lt;/beans&gt;

    spring aop 实现源代码--xml and annotation(带lib包)

    在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...

    Spring2.5和Hibernate3集成--学习spring aop ioc

    * 配置哪些类哪些方法使用事务 2.编写业务逻辑方法 * 继承HibernateDaoSupport类,使用this.HibernateTemplate这个类持久化数据 * HibernateTemplate是对session的轻量级的封装 * 默认事务回滚异常是...

    第二章:Spring AOP 基础1

    声明规则属性设置(来源于 Spring AOP Schema 类型 basicAdviceType)pointcut: Pointcut 表达式内容pointc

    spring声明式事务配置

    &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;beansxmlns=......

    Spring Aop Schema实现

    NULL 博文链接:https://bijian1013.iteye.com/blog/2136297

    开发者突击·精通AOP整合应用开发 源码

    Spring AOP:以loC为基础讲解Spring下的AOP开发,讲解了3种AOP的开发方式,即使用@Aspect注释符、基于Schema的配置的开发方式和Spring API的开发方式,最后在实际的Java EE项目中实现5种Spring AOP功能(日志记录器...

    征服Spring AOP—— Schema

    NULL 博文链接:https://snowolf.iteye.com/blog/236264

    Spring AOP 基于注解详解及实例代码

    Spring AOP 基于注解详解及实例代码 1.启用spring对@AspectJ注解的支持: &lt;beans xmlns:aop=http://www.springframework.org/schema/aop...&gt; &lt;!--启动支持--&gt; &lt;aop&gt; 也可以配置...

    spring-ibatis

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "&gt; &lt;!-- 配置视图...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;aop:config proxy-target-class="true"/&gt; &lt;tx:annotation-driven transaction-manager="txManager"/&gt; &lt;!-- 采用单数据源事务控制方式,通过注解来定义事务--&gt; class="org.springframework.jdbc....

    spring2.0声明式事务

    spring声明式事务的配置 3. spring2.0配置事务 a) 将spring 1.2升级到spring2.0 i. 去掉spring1.2相关的包 ii. 添加spring2.0的jar包:spring.jar,aspecjrt.jar,aspectjweaver.jar 和cglib-nodep-2.1.3,jar iii. ...

    struts hibernate spring 集成时使用依赖注解的方式的秘籍

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt; &lt;!-- 开启classpath扫描 --&gt; &lt;context:component-scan base-package="com.haijian" /&gt;

    Spring怎么查找xml和schema约束

    在使用Spring,写核心配置文件的时候都引入了不一样的约束,这里总结关于spring的XML约束+Schema约束。以后不管是仅使用ioc功能还是aop,把全部的约束引入即可。至于说如何找到这些约束的代码的过程,也在这个文档...

    Spring4.0+Hibernate4.0+Struts2.3整合案例

    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"&gt; ============================================= 解决整合问题: 1、Spring 3.x 对 Hibernate 4.x 不提供 HibernateDaoSupport,所以在dao的实现...

Global site tag (gtag.js) - Google Analytics