Spirng使用aspectj实现AOP的两种方式

一、AOP的基本概念:

1、AOP概念:

AOP面向切面(方面)编程,扩展功能而不修改源代码时间

AOP采取横向抽取机制, 取代了传统纵向体系重复性代码

2、AOP操作术语:

(1)连接点(Joinpoint):类里面哪些方法可以被增强,这些方法称为连接点。

(2)切入点(Pointcut):在类里面可以有很多的方法被增强,实际增强的方法称为切入点。比如实际操作中,只是增强了类里面的add方法和update方法,则add方法和update称为切入点。

(3)通知/增强(Advice):增强的逻辑,称为增强,比如扩展日志功能,这个日志功能称为增强。

        前置通知:在方法之前执行;

        后置通知:在方法之后执行;

        异常通知:在方法出现异常是执行;

扫描二维码关注公众号,回复: 2334720 查看本文章

        最终执行:在后置通知之后执行;

        环绕通知:在方法之前和之后执行。

(4)切面(Aspect):把增强应用到具体方法上面,过程称为切面,就是把增强用到切入点的过程。

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

(6)Target(目标对象):代理的目标对象,即要增强的类。

(7)Weaving(织入):是把增强应用到目标的过程,把advice应用到target的过程。

(8)Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类。

3、使用aspectj实现aop有两种方式:

(1)基于aspectj的xml配置;

(2)基于aspectj的注解方式;



二、基于aspectj的xml配置:

(1)导入相关的AOP的jar包:


(2)创建Spring核心配置文件,导入aop的约束

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

(3)使用表达式配置切入点:

常用的表达式:

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

第一个*:表示所有的修饰类型。

举例:①execution(* com.zwp.aop.User.add(..))

②execution(* com.zwp.aop.User.*(..))

③execution(* *.*(..))

④匹配所有save开头的方法:execution(* save*(..))

(4)代码测试:

//原始方法:
public class MainTest {
	public void text1(){
		System.out.println("主方法....");
	}
}
//增强的方法:
public class SecondText {

	public void before1(){
		System.out.println("前置增强");
	}
	public void after1(){
		System.out.println("后置增强");
	}
	//环绕增强
	public void round1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		//方法之前:
		System.out.println("方法之前...");
		//执行方法:
		proceedingJoinPoint.proceed();
		//方法之后:
		System.out.println("方法之后...");
	}
}

spring的applicationContext.xml配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 使用aop操作 start-->
	<!-- 1、创建两个类的对象 -->	
	<bean id="mainTest" class="com.zwp.aop.MainTest"></bean>
	<bean id="secondText" class="com.zwp.aop.SecondText"></bean>
	<!-- 2、配置aop操作 -->
	<aop:config>
		<!-- 2.1配置切点 -->
		<aop:pointcut expression="execution(* com.zwp.aop.MainTest.*(..))" id="pointcut1"/>
		<!-- 2.2配置切面:即把增强用到方法上面的过程 -->
		<aop:aspect ref="secondText">
			<aop:before method="before1" pointcut-ref="pointcut1"/>
			<aop:after method="after1" pointcut-ref="pointcut1"/>
			<aop:around method="round1" pointcut-ref="pointcut1"/>
		</aop:aspect>
	</aop:config>
	<!-- 使用aop操作 end-->
</beans>

测试类:

public class Test2 {
	@Test
	public void test4(){
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		MainTest mainTest = (MainTest) context.getBean("mainTest");
		mainTest.text1();
	}
}

运行结果:




三、基于aspectj的注解方式:

(1)导入与AOP相关的jar包:


(2)创建对象:

(3)开启Aop操作:

(4)在增强类使用注解@Aspect,在方法上使用注解完结增强配置。

测试代码:

//目标对象Target
public class Annoaop {
	public void text1(){
		System.out.println("基于aspecj的注解aop操作的主方法....");
	}
}

在spring的applicationContext.xml文件配置中,创建对象与开启AOP操作:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 基于aspecj的注解aop操作 start -->
	<!-- 1.开启aop操作 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<!-- 2.创建对象 -->
	<bean id="anno" class="com.zwp.annoAop.Annoaop"></bean>
	<bean id="add" class="com.zwp.annoAop.Add"></bean>
	<!-- 基于aspecj的注解aop操作 end -->
</beans>
//类说明:增强类
//3.1在增强类上面使用注解
@Aspect
public class Add {
	//3.2 在方法上使用注解完成增强配置:
	@Before(value="execution(* com.zwp.annoAop.Annoaop.*(..))")
	public void before1(){
		System.out.println("前置增强...");
	}
}
public class Test2 {
	@Test
	public void test5(){
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		Annoaop annoaop = (Annoaop) context.getBean("anno");
		annoaop.text1();
	}
}

运行结果:




猜你喜欢

转载自blog.csdn.net/a745233700/article/details/81006572