springAop

本文深入探讨了Spring AOP的实现原理,通过一个具体的示例详细解释了如何定义切入点、增强通知和创建代理对象。从接口声明到具体类的实现,再到通知类的编写,最后通过XML配置文件将这些元素整合在一起,展示了Spring AOP的强大功能。

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

package com.spring.jdkTest;
//接口
public interface Iuser {

	void printOne();
	void printTwo();
	}



package com.spring.jdkTest;
//目标类
public class UserImpl implements Iuser {

	@Override
	//连接点
	public void printOne() {
	System.out.println("One");	

	}

	@Override
	//连接点
	public void printTwo() {
	System.out.println("Two");	

	}
}

package com.spring.advice;
//通知
import java.lang.invoke.MethodHandleInfo;
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;



public class Advice implements org.aopalliance.intercept.MethodInterceptor{
	@Override
	public Object invoke(MethodInvocation pass) throws Throwable {
		// TODO 自动生成的方法存根
			System.out.println("前");
			Object proceed =pass.proceed();//执行目标方法
			System.out.println("后");
		return proceed;
	}

	


}

<?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"   
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:jee="http://www.springframework.org/schema/jee"  
xmlns:lang="http://www.springframework.org/schema/lang"  
xmlns:util="http://www.springframework.org/schema/util"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xmlns:mvc="http://www.springframework.org/schema/mvc"    
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   
 http://www.springframework.org/schema/jee    
 http://www.springframework.org/schema/jee/spring-jee.xsd   
 http://www.springframework.org/schema/lang    
 http://www.springframework.org/schema/lang/spring-lang.xsd   
 http://www.springframework.org/schema/context    
 http://www.springframework.org/schema/context/spring-context.xsd   
 http://www.springframework.org/schema/tx    
 http://www.springframework.org/schema/tx/spring-tx.xsd   
 http://www.springframework.org/schema/util    
 http://www.springframework.org/schema/util/spring-util.xsd   
 http://www.springframework.org/schema/mvc    
 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--设置目标类对象  -->
<bean id="targetObject" class="com.spring.jdkTest.UserImpl"></bean>
<!--增强通知类的对象  -->
<bean id="myAdvice" class="com.spring.advice.Advice"></bean>

<aop:config>
<!--aop:pointcut 看起来像连接点   -->
<!--execution(* com.xyz.myapp.service.*.*(..)) 切入点表达式  
	第一个* 号代表方法的返回值任意
	第二个* service包下的任意类
	第三个* 任意类下的任意方法
	(..)。。 方法里面的参数任意
-->

<!-- <aop:pointcut expression="execution(* com.spring.jdkTest.*.*(..))" id="myPointCut"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"/> -->
<!-- 使任意方法变为其中的一个方法,制定方法增强 -->
<aop:pointcut expression="execution(* com.spring.jdkTest.*.printOne(..))" id="myPointCut"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"/>
</aop:config>
</beans>

package com.spring.jdkTest;

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

public class AopTest{
	@Test
	public void test() {
		// TODO 自动生成的方法存根
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Iuser user = context.getBean("targetObject",Iuser.class);
		user.printOne();
		user.printTwo();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值