Spirng使用Aspectj实现AOP

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

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

运行结果:

 

二、基于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();
	}
}

运行结果:

 

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张维鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值