挖一挖Spring的 <aop:scoped-proxy/>

本文深入探讨了Spring框架中&lt;aop:scoped-proxy/&gt;的作用及其实现原理,揭示了其如何通过代理机制解决sessionbean配置问题,并追溯到了配置源头。

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

挖一挖Spring的 <aop:scoped-proxy/>

Spring 中对于 session bean 的配置有一点挺特殊:就是需要 <aop:scoped-proxy/>。 

这个原因在 Spring 的开发手册中已经详细说明了,这里仅仅是纪录一下这个东西是如何起作用的。 

从头看Spring 的代码太浪费时间了,所以直接在source中搜索“<aop:scoped-proxy/> 
”,找到这个类: 

org.springframework.aop.config.ScopedProxyBeanDefinitionDecorator 

这个类调用 ScopedProxyUtils 来生成一个代理对象。那么再看看什么地方使用了这个 ScopedProxyBeanDefinitionDecorator 类,原来是这个类: 

org.springframework.aop.config.AopNamespaceHandler 

那么在继续搜索... ... 竟然发现没有地方引用这个 AopNamespaceHandler。直觉告诉我,在一个IOC的框架中如果出现这样的情况,那么这个类应该是配置在某个文件里。不过简单看了一下,没有这样的文件。 

于是从 XmlWebApplicationContext.java 开始看吧,看啊看,看到了 AbstractXmlApplicationContext.java 。 

里面有这么一段: 

Java代码   收藏代码
  1. // Create a new XmlBeanDefinitionReader for the given BeanFactory.  
  2. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  


看类的名字,应该是读取bean定义的功能。XmlBeanDefinitionReader 里有这样一个方法: 

Java代码   收藏代码
  1. protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {  
  2.     return new DefaultNamespaceHandlerResolver(getResourceLoader().getClassLoader());  
  3. }  


结果在 DefaultNamespaceHandlerResolver 发现这么一个定义: 

Java代码   收藏代码
  1. DEFAULT_HANDLER_MAPPINGS_LOCATION = "META-INF/spring.handlers";  


打开这个文件,一切了然: 

Java代码   收藏代码
  1. http\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler  


这告诉了我几件事情: 

1、对于关键性的东西,最好有详细的注释。这样别人看代码的时候,一搜就行了,省时间。 
2、当搜索的时候,最好使用文件内字符串检索,否则,我早就发现 spring.handlers 这个文件了。


Spring AOP是Spring框架中的个重要模块,它提供了种非常方便的方法用于实现面向切面编程。在Spring AOP中,我们可以通过声明式方式来定义切面,然后将切面应用到指定的方法上,从而实现对方法的增强。 般来说,使用Spring AOP的步骤如下: 1. 定义个切面类,实现对应的切面逻辑。 2. 在配置文件中声明个切面,指定切面的类型、切点和通知。 3. 将切面应用到目标对象上,可以通过XML配置文件或者注解方式实现。 4. 测试切面的效果。 下面是个简单的示例: ```java @Aspect public class LogAspect { @Before("execution(* com.example.service.*.*(..))") public void before(JoinPoint joinPoint) { System.out.println("before"); } @AfterReturning("execution(* com.example.service.*.*(..))") public void afterReturning(JoinPoint joinPoint) { System.out.println("afterReturning"); } } @Service public class UserServiceImpl implements UserService { @Override public void addUser(User user) { System.out.println("add user"); } } <bean id="logAspect" class="com.example.aspect.LogAspect"/> <aop:config> <aop:aspect ref="logAspect"> <aop:before pointcut="execution(* com.example.service.*.*(..))"/> <aop:after-returning pointcut="execution(* com.example.service.*.*(..))"/> </aop:aspect> </aop:config> <bean id="userService" class="com.example.service.UserServiceImpl"> <aop:scoped-proxy/> </bean> ``` 在上面的示例中,我们定义了个切面类LogAspect,并将其声明为切面。然后,在XML配置文件中,我们指定了切点(execution(* com.example.service.*.*(..)))和通知(before和afterReturning),并将切面应用到UserService实现类上。 当调用UserService的addUser方法时,切面将会在方法执行前和执行后分别输出before和afterReturning。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值