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

 

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 自己动手写游戏:Flappy Bird

    一、关于FlappyBird《FlappyBird》是由来自越南的独立游戏开发者DongNguyen所开发的作品,游戏中玩家必须控制一只小鸟,跨越由各种不同长度水管所组成的障碍,而这只鸟其实是

    2021年12月19日
    42
  • 微信小程序之某荟团JS逆向

    微信小程序之某荟团JS逆向声明:本文仅限交流学习使用,请勿使用在任何非法商业活动,禁止用于非法用途。否则后果自负。如有侵权,请告知删除,谢谢!文章目录 一、fiddler抓包分析请求参数 二、微信小程序反编译 三、逆向分析与测试结果 总结 也不能说简单吧,有手就行 前言提示:以下是本篇文章正文内容,下面案例可供参考,本文仅限交流学习使用,请勿使用在任何非法商业活动,禁止用于非法用途。否则后果自负。如有侵权,请告知删除,谢谢!详细请参考这位大佬-…

    2022年6月22日
    47
  • Q1营收利润大增,Take-Two如何掘金“次世代”?[通俗易懂]

    Q1营收利润大增,Take-Two如何掘金“次世代”?[通俗易懂]8月3日美股盘后,拥有GTA和2K等知名系列游戏的Take-Two(NASDAQ:TTWO)发布了截至2020年6月30日的2020财年第一季度的业绩报告。财报公布后次日,股价跳空高开,最终股价收于177.52美元,涨幅达5.87%。回顾近期走势,TTWO已连续创下历史新高,可见其一直深受投资者青睐。(图源:雪球)以下为近期核心数据表现:由于全球疫情居家,TTWO受益颇多。本季度无论是营收、净利润,还是各产品的销量均超过市场预期。此次财报有着许多亮点值得深入讨论,而除此之外,也希望随着新品推

    2022年6月7日
    29
  • 简单易学的机器学习算法——Mean Shift聚类算法

    简单易学的机器学习算法——Mean Shift聚类算法参考文献MeanShiftClusteringMeanshift,聚类算法

    2022年7月13日
    15
  • 灰色预测模型_用excel作灰色预测步骤

    灰色预测模型_用excel作灰色预测步骤灰色预测模型是通过少量的、不完全的信息,建立数学模型并作出预测的一种预测方法。灰色系统理论是研究解决灰色系统分析、建模、预测、决策和控制的理论。灰色预测是对灰色系统所做的预测。目前常用的一些预测方

    2022年8月6日
    6
  • 【J2EE】13个规范

    【J2EE】13个规范【J2EE】13个规范

    2022年4月24日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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