spring aop _002

// 环绕型
package com.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class MethodInterceptionTest implements MethodInterceptor {

	private HibernateTemplate hiTemplate ;
	
	
	public void setHiTemplate(HibernateTemplate hiTemplate) {
		this.hiTemplate = hiTemplate;
	}


	public Object invoke(MethodInvocation m) throws Throwable {
		Object obj = null; 
		try {
			System.out.println("start .... ");
			obj =  m.proceed() ;   
			System.out.println("end ... ");
			System.out.println(m.getMethod().getName());
			System.out.println(hiTemplate.getSessionFactory().openSession().getClass().getName());
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return obj; 
	}

}


//  befor 
package com.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforMethodInterceptionTest implements MethodBeforeAdvice {

	public void before(Method arg0, Object[] arg1, Object arg2)
			throws Throwable {
		System.out.println("我执行了"+arg0.getName());

	}

}





引用

  //  环绕型 aop 配置
   <bean id="methodInterceptionTest" class="com.aop.MethodInterceptionTest" />

<aop:config>
<aop:advisor advice-ref="methodInterceptionTest" pointcut="execution (* com.service.*.*(..))"/>
</aop:config>

  //   befor aop 配置
    <bean id="beforMethodInterceptionTest" class="com.aop.BeforMethodInterceptionTest"></bean>
   <aop:config>
     <aop:advisor advice-ref="beforMethodInterceptionTest" pointcut="execution (* com.service.*.*(..))"/>
   </aop:config>  

你可能感兴趣的:(spring,AOP,orm)