Spring的AOP编程

目录

 

一、AOP开发中的相关术语

二、spring-aop编程

三、spring配置


一、AOP开发中的相关术语

1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点

    2. Pointcut(切入点)     -- 所谓切入点是指我们要对哪些Joinpoint进行拦截的定义

    3. Advice(通知/增强) -- 所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)

    4. Introduction(引介)   -- 引介是一种特殊的通知在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field

5. Target(目标对象)      -- 代理的目标对象

    6. Weaving(织入)    -- 是指把增强应用到目标对象来创建新的代理对象的过程

    7. Proxy(代理)     -- 一个类被AOP织入增强后,就产生一个结果代理类

    8. Aspect(切面)         -- 是切入点和通知的结合,以后自己来编写和配置的

二、spring-aop编程

导入spring的传统AOP开发包

spring-aop-4.2.4.RELEASE.jar

com.springsource.org.aopalliance-1.0.0.jar

aspectJ的开发包

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

spring-aspects-4.2.4.RELEASE.jar

三、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:p="http://www.springframework.org/schema/p"
    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.xsd
         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 1 创建目标类 -->
	<bean id="userServiceId" class="spring_aop3.UserServiceImpl"></bean>
	<!-- 2 创建切面类(通知) -->
	<bean id="myAspectId" class="spring_aop3.MyAdvice"></bean>
	<!-- 3 aop编程 
		3.1 导入命名空间
		3.2 使用<aop:config>进行配置
			proxy-target-class="true" 声明时使用cglib代理
			<aop:pointcut> 切入点 ,从目标对象获得具体方法
			<aop:advisor> 特殊的切面,只有一个通知 和 一个切入点
			advice-ref 通知引用
			pointcut-ref 切入点引用
		3.3 切入点表达式
			execution(* com.qf.c_spring_aop.*.*(..))
			  选择方法    返回值任意  包    类名任意 方法名任意 参数任意
		
	-->
	<aop:config proxy-target-class="true">
		<aop:pointcut expression="execution(* com.qf.c_spring_aop.*.*(..))" id="myPointCut"/>
		<aop:advisor advice-ref="advice" pointcut-ref="myPointCut"/>
	</aop:config>
</beans>

通知类写法:

public class MyAdvice implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation invoke) throws Throwable {
		Object proceed = null;
		try {	
			System.out.println("前置通知");
			proceed = invoke.proceed();
			System.out.println("后置通知");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return proceed;
	}

}

 测试类:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring_aop3/applicationContext.xml")

public class TestAOP {
	@Autowired
	private UserService UserService;
	@Test
	public void run() {	
		UserService.delete();	
	}
}


   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章