使用aspectj实现Spring的aop操作

使用aspectj实现Spring的aop操作

AOP是Spring框架中的一个重要内容,我们今天通过aspectj框架来实现Spring的aop操作。

方法一:使用表达式配置切入点。
1.准备相关的jar包
除了spring基础的包以外我们还要准备相关的aspectj包和aop包
55
2.编写类
在com.itheima.aop包中创建两个类,一个book类,一个myBook类

book.java(原业务)

package com.itheima.aop;

public class book {
	public void add() {
		System.out.println("add........");
	}

}

myBook.java(添加的业务)

package com.itheima.aop;

public class myBook {
	public void before() {
		System.out.println("before........");
	}

}

3.编写Spring配置文件
在src包中创建bean.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 	<!-- 为book、myBook类创建对象 -->
	<bean id="book" class="com.itheima.aop.book"/>
	<bean id="myBook" class="com.itheima.aop.myBook"/>
	
	<!-- 配置aop操作 -->
	<aop:config>
		<!--使用表达式配置切入点
		execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)-->
		<!-- 配置切入点 -->
		<aop:pointcut expression="execution(* com.itheima.aop.book.*(..))" id="pointcut1"/>
		<!-- 配置切面 -->
		<aop:aspect ref="myBook">
			<!-- 配置增强类型
			method:增强类里面使用哪个方法作为前置 -->
			<aop:before method="before" pointcut-ref="pointcut1"/>
		</aop:aspect>
	</aop:config>
    <!-- bean definitions here -->
</beans>

4.编写测试类
在com.itheima.aop包中创建TestAop类

package com.itheima.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestAop {
	public static void main(String[] args) {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
		book book=(book) applicationContext.getBean("book");
		book.add();
	}

}

运行
22
myBook类中的before()方法已经加载到了book的add()方法中,而且是前置通知。

方法二:基于aspecj的注解aop操作。
1.将上述中的myBook类中添加aop注释

package com.itheima.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class myBook {
	//aop前置通知
	@Before(value = "execution(* com.itheima.aop.book.*(..))")
	public void before() {
		System.out.println("before........");
	}
	//aop后置通知
	@After(value = "execution(* com.itheima.aop.book.*(..))")
	public void after() {
		System.out.println("after........");
	}

}

2.修改Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean id="book" class="com.itheima.aop.book"/>
	<bean id="myBook" class="com.itheima.aop.myBook"/>
	
	<!-- 开启aop操作 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<!--
	<aop:config>
	<aop:pointcut expression="execution(* com.itheima.aop.book.*(..))" id="pointcut1"/>
	<aop:aspect ref="myBook">
	<aop:before method="before" pointcut-ref="pointcut1"/>
	</aop:aspect>
	</aop:config>
	-->
    <!-- bean definitions here -->
</beans>

运行
44
可见前置通知和后置通知都加载完成。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值