aop的半自動和全自動動態代理

切面類:

public class MyAspect implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO 自動生成的方法存根
		
		System.out.println("前");
		 
		Object obj = invocation.proceed();
		
		System.out.println("後");
		
		return obj;
	}
}

xml核心配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
   
   <bean id="userService" class="com.spring.test5_aopProxy.UserServiceImpl"></bean>
   
   <bean id="myAspect" class="com.spring.test5_aopProxy.MyAspect"></bean>
   
   <!-- 半自動代理 -->
   <!-- 
   <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
   	<property name="interfaces" value="com.spring.test5_aopProxy.UserService"></property>
   	<property name="interceptorNames" value="myAspect"></property>
   	<property name="target" ref="userService"></property>
   </bean>
    -->
    <!-- 全自動代理方式 -->
    <aop:config>
    	<aop:pointcut expression="execution(* com.spring.test5_aopProxy.*.*(..))" id="myPointCut"/>
    	<aop:advisor advice-ref="myAspect" pointcut-ref="myPointCut"/>
    	
    </aop:config>
</beans>

在半自動代理中主要使用spring框架中的ProxyFactoryBean類來生成代理對象,該類需要獲取三個參數

參數一:<property name="interface" value="接口全限定名"/>

參數二:<property name="interceptorNames" value="切面對象ID"/>

參數三:<property name="target" ref="目標類名" /> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章