如果项目中不能使用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配置总结与案例 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45242505
Spring AOP(面向切面编程)是Java高级编程中的重要...Spring AOP的配置方式多样,包括基于接口的配置、schema-based配置和@AspectJ注解配置。通过这些配置方式,开发者可以灵活地实现AOP功能,满足不同场景下的需求。
此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的关于单元测试的支持 在类上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载...
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....
NULL 博文链接:https://quicker.iteye.com/blog/673889
第三种实现方法—通过注解来实现 签名 注解实现aop <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=... <aop:aspectj-autoproxy/> </beans>
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
* 配置哪些类哪些方法使用事务 2.编写业务逻辑方法 * 继承HibernateDaoSupport类,使用this.HibernateTemplate这个类持久化数据 * HibernateTemplate是对session的轻量级的封装 * 默认事务回滚异常是...
声明规则属性设置(来源于 Spring AOP Schema 类型 basicAdviceType)pointcut: Pointcut 表达式内容pointc
<?xml version="1.0" encoding="UTF-8"?><beansxmlns=......
NULL 博文链接:https://bijian1013.iteye.com/blog/2136297
Spring AOP:以loC为基础讲解Spring下的AOP开发,讲解了3种AOP的开发方式,即使用@Aspect注释符、基于Schema的配置的开发方式和Spring API的开发方式,最后在实际的Java EE项目中实现5种Spring AOP功能(日志记录器...
NULL 博文链接:https://snowolf.iteye.com/blog/236264
Spring AOP 基于注解详解及实例代码 1.启用spring对@AspectJ注解的支持: <beans xmlns:aop=http://www.springframework.org/schema/aop...> <!--启动支持--> <aop> 也可以配置...
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 "> <!-- 配置视图...
<aop:config proxy-target-class="true"/> <tx:annotation-driven transaction-manager="txManager"/> <!-- 采用单数据源事务控制方式,通过注解来定义事务--> class="org.springframework.jdbc....
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. ...
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 开启classpath扫描 --> <context:component-scan base-package="com.haijian" />
在使用Spring,写核心配置文件的时候都引入了不一样的约束,这里总结关于spring的XML约束+Schema约束。以后不管是仅使用ioc功能还是aop,把全部的约束引入即可。至于说如何找到这些约束的代码的过程,也在这个文档...
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> ============================================= 解决整合问题: 1、Spring 3.x 对 Hibernate 4.x 不提供 HibernateDaoSupport,所以在dao的实现...