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

Spring AOP使用配置介绍(一):增强的配置

阅读更多
在Spring中aop是一个重要的特性。和Hibernate结合的事务控制使用的就是aop,下面看如何使用。

首先定义一个增强(即通知),这是在被代理的方法执行前或执行后先执行增强中的代码再去执行被代理的方法。增强包括前置增强、后置增强、环绕增强、异常抛出增强和引介增强。
下面看一个前置增强
package com.maxjay.bank.advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
/**
 * 日志记录 前置增强
 * 
 * @author Administrator
 * 
 */
public class LoggerBeforeAdvice implements MethodBeforeAdvice {

	/**
	 * 当被代理对象的方法执行前,此方法被执行
	 * 
	 * @param method
	 *            目标类方法
	 * @param args
	 *            方法的参数
	 * @param obj
	 *            目标类实例
	 */
	public void before(Method method, Object[] args, Object obj)
			throws Throwable {
		// if (method.getName().indexOf("validateUser") != -1) { // 由advisor(切面)来定义对那些方法进行拦截
			System.out.println("日志记录开始,将要运行的方法为"
					+ obj.getClass().getSimpleName() + "." + method.getName());
		// }
	}

}


在applicationContext-aop.xml中定义这个增强
	<!-- 使用AOP进行日志记录,定义增强 -->
	<bean id="loggerBeforeAdvice" class="com.maxjay.bank.advice.LoggerBeforeAdvice" />


使用ProxyFactoryBean配置,让spring来产生这个增强后的代理bean
	<!--
		配置单个bean的代理,在使用时不能用原有bean的id要用AppContext.get("singleLoginProxy")从context中获取(见测试类LoggerAdviceTest)
	-->
	<bean id="singleLoginProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

		<property name="proxyInterfaces">
			<value>com.maxjay.bank.service.UserService</value>
		</property>

		<property name="interceptorNames">
			<list>
				<value>loggerBeforeAdvice</value>
			</list>
		</property>

		<property name="target">
			<ref bean="userService" />
		</property>

		<!-- 设置是否直接代理类(默认为false):true即使用cglib生成代理类,此时target对象不可以JDK动态代理过的bean;false则使用JDK动态代理 -->
		<property name="proxyTargetClass">
			<value>true</value>
		</property>
	</bean>

上面这段代码中的userService是普通的service类,它实现了UserService接口。这段配置就是要产生userService的代理类,而且采用的是cglib代理的方式,因此这个userService不能是JDK已经动态代理过的bean。由于我配置的这个userService已经加上了事务控制的aop增强,而且一般情况下声明式事务产生的是jdk动态代理的bean,因此会报一个无法代理的异常。如果要使用这个userService,就必须在声明事务的地方设置属性optimize为true,意思是强制使用cglib生成其代理的bean。具体配置如下:
	<!-- 事务代理 -->
	<bean id="baseTransaction" lazy-init="true" abstract="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref local="transactionManager" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="modify*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="remove*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="read*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
		<!-- 设置强制使用CGLIB生成代理 -->
		<property name="optimize" value="true" />
	</bean>


userService的配置如下:
	<bean id="userService" parent="baseTransaction">
		<property name="target">
			<bean
				class="com.maxjay.bank.service.impl.UserServiceImpl">
				<property name="userDao">
					<ref bean="userDao" />
				</property>
			</bean>
		</property>
	</bean>


测试类:
package test.com.maxjay.bank.advice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.maxjay.bank.service.UserService;

public class LoggerAdviceTest {
	public static void main(String[] args) {
		String[] paths = { "web/WEB-INF/applicationContext.xml",
				"web/WEB-INF/applicationContext-dao.xml",
				"web/WEB-INF/applicationContext-service.xml",
				"web/WEB-INF/applicationContext-aop.xml" };
		ApplicationContext ctx = new FileSystemXmlApplicationContext(paths);
		UserService service = (UserService) ctx.getBean("singleLoginProxy");
		service.validateUser("zxm", "zxm");
		//service.getAllUsers();
	}
}


此时控制台会打印:
日志记录开始,将要运行的方法为UserServiceImpl$$EnhancerByCGLIB$$3b846ea.validateUser


可以看出在调用UserService的方法时已经加入了我们想要增加的日志记录方法,到此可以学会增强的配置方法了...
1
0
分享到:
评论

相关推荐

    SpringAOP使用介绍,从前世到今生

    SpringAOP发展到现在出现的全部3种配置方式。由于Spring强大的向后兼容性,实际代码中往往会出现很多配置混杂的情况,而且居然还能工作,本文希望帮助大家理清楚这些知识。我们先来把它们的概念和关系说说清楚。AOP...

    springboot2-spring5-studying:Springboot2,Spring源码学习项目,涉及SpringBoot2自动装配实现机制,Spring AOP动态代理以及IOC各种扩展接口的实现方式

    Spring Boot 1.x版本和2.x版本AOP默认配置的移动。Spring AOP多种代理机制相关核心类介绍先介绍一些Spring Aop中一些核心类,大致分为三类: advisorCreator ,从spring ioc的扩展接口beanPostProcessor继承,主要...

    spring4.3.9相关jar包

    spring-aop.jar(必须):这个jar 文件包含在应用中使用Spring 的AOP 特性时所需的类和源码级元数据支持。使用基于AOP 的Spring特性,如声明型事务管理(Declarative Transaction Management),也要在应用里包含这...

    spring2.0技术手册

    《Spring 2.0技术手册》介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够...

    spring项目开发学习笔记

    Spring是一个轻量级的DI/IoC和AOP容器框架。存在的目的是用于构建轻量级的J2EE应用。 轻量级:应用大小和应用开支,包括应用方式 依赖注入DI/IoC控制反转:提供松耦合的一种实现技术 AOP面向切面编程:(可以在不...

    Spring2.0技术手册

    本书介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够显著减少每一位入门者...

    Spring.html

    概念:面向切面编程,在不改变源码的情况下对方法进行增强,抽取横切关注点(日志处理,事务管理,安全检查,性能测试等等),使用AOP进行增强,使程序员只需要关注与业务逻辑编写. 专业术语 目标Target:需要增强的类 ...

    Spring-IOC::label:手写实现了Spring框架的基本功能,存在了注解以及xml配置,并且实现了引用注入等功能,还实现了SpringMVC的简单功能

    我的春天Spring IOC容器简单实现,实现了一些基本的核心...适配器模式:Spring AOP的增强或通知(Advice)使用到了适配器模式,spring MVC中也是用到了适配器模式适配Controller。(体现了一点)去做: AOP实现循环依赖

    使用Spring更好地处理Struts动作

    Spring AOP 允许您使用拦截器 在一个或多个执行点上拦截应用程序逻辑。加强应用程序在拦截器中的日志记录逻辑会产生一个更可读的、实用的代码基础,所以拦截器广泛用于日志记录。您很快就会看到,为了处理横切关注点...

    使用SSM开发企业级应用第6章课后作业答案.rar

    通过接口实现增强处理是较低版本Spring AOP的做法,如果在一个使用低版本Spring AOP开发的项目上进行升级,可以考虑使用&lt;aop:advisor&gt;复用已经存在的增强类;如果项目采用JDK 5.0以上版本,可以考虑使用@AspectJ注解...

    Spring.3.x企业应用开发实战(完整版).part2

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

    Spring中文帮助文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    Spring API

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    springboot学习

    chapter3-2-11:Spring Boot中增强对MongoDB的配置(连接池等) 事务管理 chapter3-3-1:使用事务管理 chapter3-3-2:[分布式事务(未完成)] 其他内容 chapter4-1-1:使用@Scheduled创建定时任务 chapter4-1-2:...

    Spring框架生态流程框架图-执行运行路程图

    执行Spring框架的运行路程通常包括以下几个重要的步骤: 应用程序启动:应用程序启动时,Spring框架会加载并...在AOP中,开发人员可以定义切点(Pointcut)和增强(Advice),以在应用程序的不同位置插入额外的逻辑。

Global site tag (gtag.js) - Google Analytics