`
darren_nizna
  • 浏览: 71173 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

使用MethodInterceptor实现AOP

阅读更多

1. 先写出业务对象接口及实现业务对象。

 

public interface Interface {
    public void hello();
}

   这里写俩个实现。

public class InterfaceImpl implements Interface {

    /** 
     * @see com.alipay.aop.BusinessInterface#hello()
     */
    @Override
    public void hello() {
        System.out.println("AOP TEST!!");
    }
}
 

 

public class InterfaceImplTest implements Interface {

    /** 
     * @see aop.Interface#hello()
     */
    @Override
    public void hello() {
        System.out.println("helo world!!");
    }

}

 

 

2. 实现自己的代理,创建自己的interceptor

 

public class MyInterceptor implements MethodInterceptor {

    /** 
     * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
     */
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        String info = methodInvocation.getMethod().getDeclaringClass()+ "." + 
        methodInvocation.getMethod().getName() + "()";
        
        System.out.println(info);
        
        try{
            Object result = methodInvocation.proceed();
            return result;
        }
        finally{
            System.out.println(info);
        }
    }
}

 

3. 配置文件

 <?xml version="1.0" encoding="GBK"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:sofa="http://img.alipay.net/dtd/schema/service"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:webflow="http://www.springframework.org/schema/webflow-config"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://img.alipay.net/dtd/schema/service http://img.alipay.net/dtd/schema/service/sofa-service.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
         http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"
	default-autowire="byName">
	
	<bean id="beanTarget" class="aop.InterfaceImpl"/>
	
	<bean id="hello" class="aop.InterfaceImplTest" />
	
	<bean id="myInterceptor" class="aop.MyInterceptor"/>
	
	<bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>aop.Interface</value>
		</property>
		
		<property name="interceptorNames">
			<list>
				<value>myInterceptor</value>
				<value>beanTarget</value>
			</list>
		</property>
	</bean>
	
	<bean id="testBean" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>aop.Interface</value>
		</property>
		
		<property name="interceptorNames">
			<list>
				<value>myInterceptor</value>
				<value>hello</value>
			</list>
		</property>
	</bean>
</beans>
 

 

 

4. 测试代码

 

 

public class Main {

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        ClassPathResource resource = new ClassPathResource("spring.xml");
        XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
        Interface bean = (Interface)beanFactory.getBean("bean");
        bean.hello();
        
        Interface testBean = (Interface)beanFactory.getBean("testBean");
        testBean.hello();
    }
}
3
7
分享到:
评论
3 楼 nhy338 2012-08-28  
mark,留着以后看
2 楼 爪哇夜未眠 2011-11-18  
这个是spring里面的方法拦截
1 楼 sunnylocus 2011-10-13  
做下标记,明天有空仔细研究下方法拦截,感觉这是好东西!

相关推荐

    org/aopalliance/intercept/MethodInterceptor 类 fro Spring3.0

    nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor 就是少了这个包

    spring aop实现

    spring,aop的实现.MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor等。

    spring aop 实现源代码--xml and annotation(带lib包)

    实际上,如果需要在业务代码执行前后增加额外的服务,你可以直接通过实现org.aopalliance.intercept.MethodInterceptor接口来达到这一目的,MethodInterceptor定义如下: java 代码 1. package org.aopalliance....

    spring boot如何使用spring AOP实现拦截器

    本篇文章主要介绍了spring boot如何使用spring AOP实现拦截器,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    aopalliance-1.0.jar

    AOP联盟为Java的AOP提供了一系列标准接口,包括Advice通知及其继承接口MethodInterceptor方法拦截器,还有JointPoint连接点及其继承接口MethodInvocation。例如,很多具有AOP概念的框架如Spring AOP都会依赖于此来...

    spring,hibernate整合实现事务管理(MethodInterceptor)

    NULL 博文链接:https://88548886.iteye.com/blog/1528486

    类似spring Aop的Proxy封装

    有一天在用回调模版的时候,忽然间又想到jdk自带的Proxy类似乎不是怎么好用,...这样,在使用的时候只要实现其MethodInterceptor接口即可。省去了去实现jdk的InvocationHandler,而且用户只关心对方法进行怎样的处理.

    aopalliance.jar

    aopalliance.jar spring3.0 所需

    spring aop 实例

    spring aop 四种模式 1、AfterReturningAdvice 2、MethodInterceptor 3、MethodBeforeAdvice 4、ThrowsAdvice

    java cglib和反射demo

    实现MethodInterceptor简单的aop 分别用cglib和反射实现了一下

    springAOP demo 带错误解决文档

    在搭建spring项目时通常需要这些jar包 commons-logging-1.1.3.jar spring-aop-4.0.6.RELEASE.jar ... nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

    13Spring知识点1

    反射:动态代理设计模式 1.jdk 依赖接口 newProxyInstance方法 2.cglb 实现MethodInterceptor Aop 四种通知之前通

    Spring Boot Aspect 切面 AOP 拦截器 Interceptor 监控control请求耗时

    常用拦截 拦截器HandlerInterceptor 拦截器MethodInterceptor 添加依赖 创建启动类 创建拦截器类 创建控制器 监控control请求耗时,提高性能

    Java Web程序设计教程

    12.2.3methodinterceptor 249 12.2.4throwadvice 250 12.3使用spring的切入点 251 12.3.1静态切入点 251 12.3.2动态切入点 253 12.4springaop的代理工厂 253 12.4.1选择合适的代理 253 12.4.2proxyfactory ...

    Spring.net框架

    (4)使用Spring.net实现Ioc; (5)Romoting; (6)利用Ioc在不动一行代码的情 况下实现Remoting。为了更好的理解文中的内容,最好顺序阅读。 作为一个应用系统,代码复用至关重要。如果在你的设计中,类与类存在...

    java中的0 can‘t find referenced pointcut runTim

    后来经过排查,发现是自己的写法有问题,在@Around的参数中我们使用的是runtime(),这里是有问题的! @Around: 环绕增强,相当于MethodInterceptor. 这里Around的value参数应该写的是上面的签名,而不是runTime,...

    mobileSenderDemo

    Spring Data Jpa应用程序API API1.1。 기본API가인터페이스상속하여사용 반스않고스스스스스스함함함... AOP倒影기술을활용함 ProxyFactory,反射,MethodInterceptor,JdkDynamicAopProxy,CGLib Spring数据Jpa소스를

    java8源码-springboot_gradle_demos:spring-bootgradle演示

    java8 源码 所有项目demo都基于idea gradle + SpringBoot来开发构建 ...@Around:环绕增强,相当于MethodInterceptor @AfterReturning:后置增强,相当于AfterReturningAdvice,方法正常退出时执行 @Before

Global site tag (gtag.js) - Google Analytics