如何在Spring中连接代理对象?

发布于 2024-10-13 10:01:23 字数 1469 浏览 6 评论 0原文

我正在使用带有属性可选传输协议的代理 bean。我的问题是bean属性无法转换,但我真的不知道为什么。情况是这样的:

我的属性:service.protocol=rmi

<!-- This is the 'multiplexing' factory bean (using this because properties
cannot be used in bean names and aliases -->

   <bean name="dbFormGenWindowComponent" 
  class="org.springframework.beans.factory.config.BeanReferenceFactoryBean">
  <property name="targetBeanName" value="dbFormGenWindowComponent-${service.protocol}invoker" />
 </bean>

<!-- Here are the service invoker beans with two protocols: -->

  <bean name="dbFormGenWindowComponent-rmiinvoker" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
 <property name="serviceUrl" value="${ringwindow.serviceURL.rmi}/${ringwindow.service.name}-dbFormGenWindowComponent"/>
 <property name="serviceInterface" value="foo.bar.service.formgen.windows.FormGenWindowComponent" />
 <property name="lookupStubOnStartup" value="false"/>
  </bean>

启动时的异常是:

org.springframework.beans.TypeMismatchException: 无法转换属性值 输入 [$Proxy541] 到所需类型 [foo.bar.service.formgen.windows.FormGenWindowComponent] 对于属性“formGenWindowComponent”; 嵌套异常是 java.lang.IllegalArgumentException: 无法转换类型值 [$Proxy541] 为所需类型 [foo.bar.service.formgen.windows.FormGenWindowComponent] 对于属性“formGenWindowComponent”: 没有匹配的编辑器或转换 找到策略

我认为嵌套工厂 bean 应该可以正常工作。您知道如何完成这项工作吗?

I am using proxy beans with property-selectable transport protocol. My problem is that bean properties cannot be converted, but I really don't know why. This is the situation:

My property: service.protocol=rmi

<!-- This is the 'multiplexing' factory bean (using this because properties
cannot be used in bean names and aliases -->

   <bean name="dbFormGenWindowComponent" 
  class="org.springframework.beans.factory.config.BeanReferenceFactoryBean">
  <property name="targetBeanName" value="dbFormGenWindowComponent-${service.protocol}invoker" />
 </bean>

<!-- Here are the service invoker beans with two protocols: -->

  <bean name="dbFormGenWindowComponent-rmiinvoker" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
 <property name="serviceUrl" value="${ringwindow.serviceURL.rmi}/${ringwindow.service.name}-dbFormGenWindowComponent"/>
 <property name="serviceInterface" value="foo.bar.service.formgen.windows.FormGenWindowComponent" />
 <property name="lookupStubOnStartup" value="false"/>
  </bean>

The exception on startup is:

org.springframework.beans.TypeMismatchException:
Failed to convert property value of
type [$Proxy541] to required type
[foo.bar.service.formgen.windows.FormGenWindowComponent]
for property 'formGenWindowComponent';
nested exception is
java.lang.IllegalArgumentException:
Cannot convert value of type
[$Proxy541] to required type
[foo.bar.service.formgen.windows.FormGenWindowComponent]
for property 'formGenWindowComponent':
no matching editors or conversion
strategy found

I think nested factory beans should work fine. Do you have any idea how to get this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-10-20 10:01:23

当您将注入点类型定义为具体类而不是接口,但您是基于接口进行代理时,通常会发生这种情况。例如:

public interface Foo { .. }
public class FooImpl { .. } // this is declared as bean

public class Bar {
    private FooImpl foo; // this fails
    private Foo foo; // correct way
}

对于工厂 bean,这可能是由于工厂 bean 的返回类型被定义为具体类。如果你不能改变类中的任何内容,你可以配置 spring 使用 cglib-proxying,通过:

  • - 在 bean 定义中 - 这将配置bean
  • - 全局更改此设置

This usually happens when you have defined your injection point types to be concrete classes, rather than interfaces, but you are proxying based on interface. For example:

public interface Foo { .. }
public class FooImpl { .. } // this is declared as bean

public class Bar {
    private FooImpl foo; // this fails
    private Foo foo; // correct way
}

In the case of factory beans this may be due to the factory bean's return type being defined as a concrete class. If you can't change anything in the classes, you can configure spring to use cglib-proxying, by:

  • <aop:scoped-proxy> - inside a bean definition - this will configure proxies of the bean
  • <aop:aspectj-autoproxy proxy-target-class="true"> - changes this globally
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文