`
8821249
  • 浏览: 67343 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring笔记(九)Spring aop的两种实现

阅读更多
一、目标对象实现代理接口,使用动态代理机制(JDK的proxy)
  1、由于JDK的代理方式要求目标对象实现了接口。
  2、相关类java.lang.reflect.Proxy、java.lang.reflect.InvocationHandler。

二、若目标对象未实现代理接口,使用动态代理机制(CGLIB项目)
  1、CGLIB通过生成目标对象的子类。

三、两种实现的比较
  1、CGLIB生成的代理对象运行的快。
  2、JDK生成代理所用的时间少。

四、强制代理方式
  1、一般情况下Spring会自动分析目标类是否实现了接口,自动选择两种方式。
  2、要强制,可以将代理类org.springframework.aop.framework.ProxyFactoryBean的proxyTargetClass属性为设为true,Spring就会用CGLIB代理,否则用JDK代理。
	<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<!-- 若目标对象实现了代理接口,则可以提供代理接口的配置 -->
		<property name="proxyInterfaces"  value="aop.spring.ICustomerServiceProxy" />
		<!-- 配置目标对象 -->
		<property name="target" ref="customerServiceTarget" />
		<!-- 配置切面 -->
		<property name="interceptorNames">
			<list>
				<value>logIntercepter</value>
			</list>
		</property>
		[b]<property name="proxyTargetClass" value="true"></property>[/b]
	</bean>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics