// 环绕型
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());
}
}
[quote]
// 环绕型 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>
[/quote]
本文介绍了一种使用Spring AOP实现环绕增强与前置通知的方法。通过具体代码示例展示了如何定义环绕型拦截器MethodInterceptionTest及前置通知BeforMethodInterceptionTest,并配置它们来对指定的目标方法进行增强。
6490

被折叠的 条评论
为什么被折叠?



