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

Spring -- 使用MethodInterceptor实现AOP

 
阅读更多
.1. 先写出业务对象接口及实现业务对象。




1.public interface Interface {   
2.    public void hello();   
3.}  
public interface Interface {
    public void hello();
} 
 
这里写俩个实现。



1.public class InterfaceImpl implements Interface {   
2.  
3.    /**   
4.     * @see com.alipay.aop.BusinessInterface#hello()  
5.     */  
6.    @Override  
7.    public void hello() {   
8.        System.out.println("AOP TEST!!");   
9.    }   
10.} 

 


1.public class InterfaceImplTest implements Interface {   
2.  
3.    /**   
4.     * @see aop.Interface#hello()  
5.     */  
6.    @Override  
7.    public void hello() {   
8.        System.out.println("helo world!!");   
9.    }   
10.  
11.}  



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



1.public class MyInterceptor implements MethodInterceptor {   
2.  
3.    /**   
4.     * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)  
5.     */  
6.    @Override  
7.    public Object invoke(MethodInvocation methodInvocation) throws Throwable {   
8.        String info = methodInvocation.getMethod().getDeclaringClass()+ "." +    
9.        methodInvocation.getMethod().getName() + "()";   
10.           
11.        System.out.println(info);   
12.           
13.        try{   
14.            Object result = methodInvocation.proceed();   
15.            return result;   
16.        }   
17.        finally{   
18.            System.out.println(info);   
19.        }   
20.    }   
21.}
 

3. 配置文件
<?xml version="1.0" encoding="GBK"?>

1.<beans xmlns="http://www.springframework.org/schema/beans"  
2.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
3.    xmlns:p="http://www.springframework.org/schema/p"  
4.    xmlns:sofa="http://img.alipay.net/dtd/schema/service"  
5.    xmlns:context="http://www.springframework.org/schema/context"  
6.    xmlns:webflow="http://www.springframework.org/schema/webflow-config"  
7.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
8.         http://img.alipay.net/dtd/schema/service http://img.alipay.net/dtd/schema/service/sofa-service.xsd   
9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
10.         http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"   
11.    default-autowire="byName">   
12.       
13.    <bean id="beanTarget" class="aop.InterfaceImpl"/>   
14.       
15.    <bean id="hello" class="aop.InterfaceImplTest" />   
16.       
17.    <bean id="myInterceptor" class="aop.MyInterceptor"/>   
18.       
19.    <bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">   
20.        <property name="proxyInterfaces">   
21.            <value>aop.Interface</value>   
22.        </property>   
23.           
24.        <property name="interceptorNames">   
25.            <list>   
26.                <value>myInterceptor</value>   
27.                <value>beanTarget</value>   
28.            </list>   
29.        </property>   
30.    </bean>   
31.       
32.    <bean id="testBean" class="org.springframework.aop.framework.ProxyFactoryBean">   
33.        <property name="proxyInterfaces">   
34.            <value>aop.Interface</value>   
35.        </property>   
36.           
37.        <property name="interceptorNames">   
38.            <list>   
39.                <value>myInterceptor</value>   
40.                <value>hello</value>   
41.            </list>   
42.        </property>   
43.    </bean>   
44.</beans>  






4. 测试代码
1.public class Main {   
2.  
3.    /**  
4.     *   
5.     * @param args  
6.     */  
7.    public static void main(String[] args) {   
8.        ClassPathResource resource = new ClassPathResource(&quot;spring.xml&quot;);   
9.        XmlBeanFactory beanFactory = new XmlBeanFactory(resource);   
10.        Interface bean = (Interface)beanFactory.getBean(&quot;bean&quot;);   
11.        bean.hello();   
12.           
13.        Interface testBean = (Interface)beanFactory.getBean(&quot;testBean&quot;);   
14.        testBean.hello();   
15.    }   
16.} 


分享到:
评论

相关推荐

    springAOP demo 带错误解决文档

    spring-aop-4.0.6.RELEASE.jar spring-beans-4.0.6.RELEASE.jar spring-context-4.0.6.RELEASE.jar spring-core-4.0.6.RELEASE.jar spring-expression-4.0.6.RELEASE.jar 在使用spring额外功能时 1 需要在xml文件中...

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

    在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...

    spring aop实现

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

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

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

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

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

    类似spring Aop的Proxy封装

    有一天在用回调模版的时候,忽然间又想到jdk自带的Proxy类似乎不是怎么好用,为何不把其做成回调模版呢,可以将其改造称类似spring Aop 代码如下: package com.fjx.proxy.up.test; import ...

    aopalliance-1.0.jar

    aopalliance-1.0.jar是一个Java库,提供了一套AOP(面向切面编程)的接口和注解,这可以用于在Java应用程序中实现横切关注点的模块化。...例如,很多具有AOP概念的框架如Spring AOP都会依赖于此来实现其功能。

    spring aop 实例

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

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

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

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

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

    aopalliance.jar

    aopalliance.jar spring3.0 所需

    Spring.net框架

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

    java8源码-springboot_gradle_demos:spring-bootgradle演示

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

    13Spring知识点1

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

    Java Web程序设计教程

    12.4springaop的代理工厂 253 12.4.1选择合适的代理 253 12.4.2proxyfactory 254 12.4.3proxyfactorybean 254 12.5项目实战——输出日志 256 本章小结 258 课后练习 259 第13章 spring与javaee持久化数据...

    mobileSenderDemo

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

Global site tag (gtag.js) - Google Analytics