在Spring AOP介绍中丢失了原始界面 [英] Original interface is lost in Spring AOP introduction

查看:155
本文介绍了在Spring AOP介绍中丢失了原始界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Spring AOP配置。

Here is my Spring AOP configuration.

<bean id="myObject" class="com.madzone.learn.spring.aop.OriginalClass"></bean>
<bean id="aspect" class="com.madzone.learn.spring.aop.AspectClass"></bean>
<aop:config>
    <aop:aspect ref="aspect">
        <aop:declare-parents
            types-matching="com.madzone.learn.spring.aop.OriginalClass+"
            implement-interface="com.madzone.learn.spring.aop.IntroducedInterface"
            default-impl="com.madzone.learn.spring.aop.IntroducedInterfaceImpl" />
    </aop:aspect>





ApplicationContext context = new ClassPathXmlApplicationContext("myApp.xml");
Object myObject = context.getBean("myObject");
if (myObject instanceof OriginalClass) {
    System.out.println("This is OriginalClass");
}
if(myObject instanceof IntroducedInterface) {
    System.out.println("This is IntroducedInterface");
}

通过这个介绍,我能够调用 IntroducedInterface中的方法。但是,我无法访问OriginalClass的方法。在上面的代码片段中,我从未打印出'This is OriginalClass'。

With this introduction I was able to call the methods in the IntroducedInterface. But, I was not able to access the OriginalClass' methods. In the code snippet above, I never got the 'This is OriginalClass' printed out.

从'Introduction'的定义我理解实现新接口的代理将从OriginalClass扩展并使其'方法也可访问。

From the definition of 'Introduction' I understood that the proxy that implements the new interface will extend from OriginalClass and make its' methods accessible too.

我在这里遗漏了什么吗?有人可以解释原因吗?

Am I missing something here? Can someone explain the reasons, if any?

PS:
以下是Spring in Action(第3版)中描绘的图片。

PS: The following is a picture from Spring in Action (3rd Edition) that depicts this.

推荐答案


从'Introduction'的定义我理解
实现新接口的代理将从OriginalClass扩展并使
'也可以访问'方法。

From the definition of 'Introduction' I understood that the proxy that implements the new interface will extend from OriginalClass and make its' methods accessible too.

我不确定你从哪里获得这种印象。默认情况下,所有Spring AOP都是在上构建的JDK动态代理,仅适用于接口。代理一个具体的课是不可能的。在Spring中支持使用CGLIB代理来代替类,但它的用法是不鼓励参考指南支持编程接口以减少耦合。

I'm not sure where you got that impression from. All of Spring AOP is built, by default, on JDK dynamic proxies, which only work for interfaces. It's impossible to proxy a concrete class. There is support in Spring for using CGLIB proxies in order to proxy classes instead, but its use is discouraged by the reference guide in favor of programming to interfaces to reduce coupling.

这篇关于在Spring AOP介绍中丢失了原始界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆