Spirng使用Aspectj实现AOP

Spirng使用Aspectj实现AOP

Aspectj实现AOP有两种方式:

(1)基于aspectj的xml配置;

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

 

一、基于aspectj的xml配置:

1、导入相关的AOP的jar包:

Spirng使用Aspectj实现AOP

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、使用表达式配置切入点:

(1)表达式格式:

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

(2)常用的表达式举例:

举例:

①execution(* com.zwp.aop.User.add(..))

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

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

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

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

(3)代码测试:

//原始方法:
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();
	}
}

运行结果:

Spirng使用Aspectj实现AOP

 

二、基于aspectj的注解方式:

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

Spirng使用Aspectj实现AOP

(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();
	}
}

运行结果:

Spirng使用Aspectj实现AOP

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/114731.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理

    Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理

  • 什么是静态代理和动态代理,两者的区别(笔记)「建议收藏」

    什么是静态代理和动态代理,两者的区别(笔记)「建议收藏」文章目录1.什么是代理?2.静态代理3.动态代理4.总结:1.什么是代理?​ 代理:就是让代理角色帮助真实角色完成一件事情;​ 举例:过年回家让朋友代买火车票,朋友帮你买火车票的过程就是代理2.静态代理​ 什么是静态代理:静态代理相当于是多写了一个代理类,在调用的时候调用的是代理类,在代理类中的处理还是原生的处理逻辑,不过在前后添加上需要添加的代码。缺点:需要为每一个被代理的对象都创建一个代理类。​ 特点:​ 代理角色和真实角色都需要实现同一个接口,​ 真实角色专注于自己的.

    2022年10月19日
  • Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败

    Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败

    2021年10月20日
  • java有全局变量吗_java局部变量和成员变量的区别

    java有全局变量吗_java局部变量和成员变量的区别//    java全局变量危害 //    最近为了图快捷,使用了全局变量,然后就想到了一些危害//  1.线程不安全:线程中多个全局变量,修改容易冲突,需要加锁//  2.增加耦合性:修改全局变量可能会影响其他模块//  3.难以定位修改:难以定位全局变量在哪里被修改了,加大了调用难度//  4.长期占用内存:生命

  • 指令字长,机器字长,存储字长的关系_指令字长的概念

    指令字长,机器字长,存储字长的关系_指令字长的概念指令字长、存储字长、机器字长、时钟周期、机器周期、指令周期、取址周期、存取周期的关系考研做题途中遇到这些问题,发现自己掌握的很模糊,遂写下此篇,加深记忆。1、机器字长、存储字长、指令字长机器字长:CPU一次能够处理的数据的位数。通常等于寄存器的位数。例子:windows64位/32位,这里的64位和32位指的就是该操作系统的机器字长。存储字长:计算机存储器中一个存储单元可以存储的位数。例子:某某计算机按照字节编址,即说明该计算机的存储字长为1B=8位。指令字长:计算机内一条指令的位数。这里通常指

  • python 怎么保留小数「建议收藏」

    python 怎么保留小数「建议收藏」使用字符串格式化大部分语言都可以使用字符串格式化的方法来实现保留两位小数的效果,python也不例外:a=3.1415926print(“%.2f”%a)#%代表格式化输出,.2代表小数点后保留两位,f代表数据类型是浮点型使用round内置函数python内置了一个名为round的函数,这个函数可以用来对数据进行格式化。代码如下:a=3.1415926a1=round(a,2)#将a通过round函数处理后赋值给a1,传入的2代表保留两位小数print(a1)使

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号