SPring AOP

本文介绍了一个使用 Spring AOP 的具体实现案例,包括定义接口 IExam 和其实现类 QiangShou,以及 Before 和 After 类来实现前置和后置通知。通过配置 XML 文件实现了对方法 test6 的增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录结构:



上代码:

package aop;

public interface IExam {
	public abstract void test4();
	public abstract void test6();

}


package aop;

public class QiangShou implements IExam{

	
	public void test4() {
		
		System.out.println("保证通过四级");
	}

	
	public void test6() {
		
		System.out.println("保证通过六级");
	}

}


package aop;

public class After {
	public void af(){
		System.out.println("给你证书");
	}

}





package aop;

public class Before {
	public void bf(){
		System.out.println("请给我代理费");
	}
	

}


package aop;

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

public class TestAOP {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		IExam cbb=new QiangShou();
		
		Proxy46 proxy=new Proxy46();
		proxy.setQs(cbb);
		proxy.test4();
		*/
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		IExam exam=context.getBean("target",IExam.class);
		exam.test6();

	}

}



<?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:p="http://www.springframework.org/schema/p" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
		
	<!-- 配目标 -->
	<bean id="target" class="aop.QiangShou"></bean>
	
	<!-- 配通知 -->
	<bean id="bfadvice" class="aop.Before"></bean>
	<bean id="afadvice" class="aop.After"></bean>
	
	<!-- 配切面(aspect) -->
	<aop:config>
		<!-- 切入点 -->
		<aop:pointcut expression="execution(* aop.QiangShou.*6(..))" id="mypoint"/>
		<!-- 引用通知 -->
		<aop:aspect ref="bfadvice">
			<aop:before method="bf" pointcut-ref="mypoint"/>
		</aop:aspect>
		<aop:aspect ref="afadvice">
			<aop:after method="af" pointcut-ref="mypoint"/>
		</aop:aspect>
	</aop:config>
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

野火少年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值