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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • keyvaluepair_KeyValuePair用法(转)

    keyvaluepair_KeyValuePair用法(转)C#KeyValuePair的用法。结构体,定义可设置或检索的键/值对。也就是说我们可以通过它记录一个键/值对这样的值。比如我们想定义一个ID(int类型)和Name(string类型)这样的键/值对,那么可以这样使用。//////设置键/值对//////privateKeyValuePairSetKeyValuePair(){intintKey=1;stringstrV…

    2022年7月26日
    6
  • 05_Spring Cloud Alibaba Dubbo

    05_Spring Cloud Alibaba Dubbo

    2021年7月11日
    66
  • ORA-01017解决方案「建议收藏」

    ORA-01017解决方案「建议收藏」ora-01017是用户登录的报错。解决思路:1)确认所登用户的状态。可能是被锁了,可能是密码过期状态。修改之,即可2)当然是确认用户名密码是否输入正确。不确定密码的话可以重设。3)oracle-12C有了数据库容器概念。所登用户是否在PDBORCL里,tnsnames.ora文件里是否配置了PDBORCL,登录时是否选中了PDBORCL4)所登用户是否是sysdba。是的话登录语句要加assysdba这4步确定好了,能解决100%的ora-01017报错情况。…

    2022年5月31日
    75
  • SQLite数据库中文乱码处理「建议收藏」

    SQLite数据库中文乱码处理「建议收藏」通过SQLiteAdministrator等工具生成的数据库文件,放入到Android中,查询记录怎么也查不出来,后来发现是编码错误,SQLiteAdministrator不支持utf编码,所以存储的中文会出现乱码的情况,处理方法如下:SQLiteDatabasedb=dbHelper.getWritableDatabase();Cursorcursor

    2022年9月3日
    4
  • 线性回归目标函数—–最小二乘法推导过程「建议收藏」

    线性回归目标函数—–最小二乘法推导过程「建议收藏」在学习线性回归的时候

    2022年5月16日
    41
  • ram和rom的区别_RAM和ROM各有什么特点

    ram和rom的区别_RAM和ROM各有什么特点RAM和ROM总结一、在解释之前先备注一些缩写的全称便于记忆:1、EPROM:(ElectricallyProgrammableRead-Only-Memory)电可编程序只读存储器2、EE

    2022年8月1日
    3

发表回复

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

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