三、Spring AOP

前置通知
示例:
//在addStudent()方法加前置通知

package com.service.impl;
public class StudentImpl {
	public  void addStudent() {
		System.out.println("add");
	}
}

步骤一:
添加jar包:
aopaliance.jar
aspectjweaver.jar

步骤二:编写LogBefore()方法

package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class LogBefore implements MethodBeforeAdvice{//实现特定接口,将普通类变为具有特定功能的类

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		System.out.println("LogBefore");
		
	}
}

步骤三:配置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-4.2.xsd">
	<bean id="logBefore" class="com.aop.LogBefore">
	
	</bean>
	<bean id="logAfter" class="com.aop.LogAfter">
	
	</bean>
	
	<bean id="studentImpl" class="com.service.impl.StudentImpl">
	
	</bean>
	
	<aop:config>
		<aop:pointcut expression="execution(public void com.service.impl.StudentImpl.addStudent())" id="pointcut"/>//切入面
		<aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/>//连接线:前置通知------切入面id
	</aop:config>
</beans>
发布了16 篇原创文章 · 获赞 0 · 访问量 228

猜你喜欢

转载自blog.csdn.net/BTLA_2020/article/details/105202992