AOP基础知识及AOP切面编程之注释方法、xml配置方法

前端之家收集整理的这篇文章主要介绍了AOP基础知识及AOP切面编程之注释方法、xml配置方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

AOP概念

Aspect(切面):它跟类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是对横切性关注点的抽象

joinpoint(连接点):所谓连接点就是被拦截到的点,在spring中,这些点是方法,因为spring只支持方法类型的连接点,实际上joinpoint还可以是field或类构造器.

pointcut(切入点):就是要对哪些点进行拦截的定义。

advice(通知):拦截到的joinpoint之后所要做的事情就是通知通知分为前置通知,后置通知,异常通知,最终通知,环绕通知通知就是要做的事情。

target(目标对象):代理的目标对象

weave(织入):指将aspect应用到target对象并导致proxy对象创建的过程称为织入。

Introduction(引入);在不修改代码的前提下,Introduction可以在运行期动态地添加一些方法或者field.

AOP使用到的jar包

spring.jar

commons-logging.jar

aspectjweaver.jar

aspectjrt.jar

cglib-nodep-2.1.3.jar

如果使用了SR250中的注解,如@Resource/@PostConstruct/@PreDestroy的还要引入common-annotation.jar


AOP命名空间的配置

使用spring框架aop要引入下面的aop命名空间(红色部分)

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

aop:aspectj-autoproxy/>

</beans

下面是使用注释法的aop切面编程的示例的主要代码

package com.gdhdcy.service;

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;

@Aspect
public class MyInterceptor {
	
	@Pointcut("execution (* com.gdhdcy.service.impl.PersonServiceBean.*(..))")
	private void anyMethod(){};//生命一个切入点
    
	@Before("anyMethod() && args(name)")   //填写的是切入点的方法名称,记得要加上()
	public void doAccessCheck( String name){
		System.out.println("前置通知"+name);
	}
	
	//@AfterReturning("anyMethod()")
	@AfterReturning(pointcut="anyMethod()",returning="result")  //得到返回值
	public void doAfterReturning(String result){
		System.out.println("后置通知"+result);
	}
	
	@After("anyMethod()")
	public void doAfter(){
		System.out.println("最终通知");
	}
	
	//@AfterThrowing("anyMethod()")
	@AfterThrowing(pointcut="anyMethod()",throwing="e")
	public void doAfterThrowing(Exception e){
		System.out.println("例外通知"+e);
	}
	
	@Around("anyMethod()")
	public Object doAround(ProceedingJoinPoint pjp)throws Throwable{
		System.out.println("环绕通知进入");
		Object result=pjp.proceed();
		System.out.println("环绕通知退出");
		return result;
		
	}
}

注释方法beans.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:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       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="myInterceptor" class="com.gdhdcy.service.MyInterceptor"/>
		<bean id="personService" class="com.gdhdcy.service.impl.PersonServiceBean"/>
</beans>

附源码链接

http://pan.baidu.com/s/1jGmQMjG


xml方法aop切面编程的示例的主要代码

package cn.itcast.service;

import org.aspectj.lang.ProceedingJoinPoint;
/**
 * 切面
 *
 */
public class MyInterceptor {	
	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 result = pjp.proceed();
		System.out.println("退出方法");
		return result;
	}
	
}

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:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       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="com.gdhdcy.service.impl.PersonServiceBean"></bean>
        <bean id="aspetbean" class="com.gdhdcy.service.MyInterceptor"/>
        <aop:config>
        	<aop:aspect id="asp" ref="aspetbean">
        		<aop:pointcut id="mycut" expression="execution(* com.gdhdcy.service..*.*(..))"/>
        		<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
        		<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>

猜你在找的XML相关文章