- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Spring 中有一个 bean 定义,它是代理对应物,可以在任何地方使用:
<bean name="my.Bean" class="org.springframework.aop.framework.ProxyFactoryBean" scope="prototype">
<property name="proxyInterfaces" value="my.Interface"/>
<property name="target" ref="my.BeanTarget"/>
<property name="interceptorNames">
<list>
<value>someInterceptor</value>
</list>
</property>
</bean>
<bean name="my.BeanTarget" class="my.InterfaceImpl" scope="prototype">
<property name="foo" ref="bar"/>
</bean>
一切正常;在 pre-Spring v3 世界中,我像这样使用它
ApplicationContext ctx = ...;
my.Interface foo = (my.Interface) ctx.getBean("my.Bean"); // cast is necessary
在 Spring 3 中,可以进行类型安全查找,例如:
my.Interface foo = ctx.getBean(my.Interface.class);
同样,这对普通 bean 很有效,而对于代理 bean,我得到的是 my.BeanTarget
而不是 my.Bean
。我试图内联 my.BeanTarget
(如 Spring 文档中所示)以使其隐藏,但我得到的只是
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [my.Interface] is defined: expected single bean but found 0:
那么是否可以对代理 bean 使用类型安全的 bean 查找,如果是的话 - 如何实现?
最佳答案
这里的问题是 ProxyFactoryBean
上的 scope="prototype"
。
上下文只会急切地初始化单例 bean 定义。非单例范围的 bean 仅在需要时才初始化。这意味着当您向上下文询问给定类型的 bean 时,上下文无法初始化那些非单例 bean 以询问它们的类型,它必须完全依赖 bean 定义中的信息。
在 ProxyFactoryBean
的情况下,生成的代理的类型由需要完全初始化 bean 的复杂逻辑决定。如果没有该初始化,ProxyFactoryBean
只能将目标类型报告为 null
。
除了使用单例 bean 定义,或者通过名称明确请求 bean 之外,我不能说解决这个问题的方法,例如
<bean id="my.Interface"> class="ProxyFactoryBean"... >
然后:
ctx.getBean(MyInterface.class.getName());
在这里,我们使用 bean 名称的约定作为它们实现的接口(interface)。
关于java - ApplicationContext.getBean(Class clazz) 不适用于代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3574092/
构建中断后,一位开发人员提示说这是因为以前可用的 Clazz.clone()(不是类的真实名称 :))已被删除,所以他调用 clz.clone() 正在中断。 据此Clazz 的作者建议将客户端代码更
我有两个作用于 Clazz 类型对象的函数,它们之间的唯一区别是我访问其成员的方式: void foo(vector& v) { ... v[0]->method(); ...
我需要在 java 中使用反射。我明白 Class clazz创建一个代表 Class 的变量目的。但是,我正在尝试引用 Class来自 String 的对象使用 forName("aClassNam
我正在研究 Snappy、Zlib 等的压缩器实现(Java 中)。靠近源文件顶部的是下面的这一行。谁能给我解释一下这是什么意思? HACK - Use this as a global lock i
所以我知道 Python 的 typing.Optional。但是我自己编写了粗略的 PyOptional(代码 here )并想将 Optional[T] 与我的 PyOptional 结合到 Py
我需要一些关于简单正则表达式的帮助。目标是简化 Java 方法的参数。 输入:java.util.Locale arg0, java.lang.String arg1, java.lang.Objec
我正在尝试调用 EasyMock.isA(Class)在 List 上.有没有办法在没有警告的情况下做到这一点? 我试过以下方法: isA(List.class); // doesn't compi
如何Class clazz看起来像 Kotlin ? 我正在尝试将此方法转换为 Kotlin 代码,但似乎卡住了。 public static boolean isServiceRunning(Con
我已经开发 Android 应用程序大约 1 个月了,现在我已经非常熟悉 Java 语法了,但今天我偶然发现了这段代码: try { Class clazz =
我必须为 GWT/GXT 项目开发一个“通用”wigdet,为此我需要创建一个类型未知的对象实例。我找到了一种在开发模式下完美运行的方法,但是当我尝试编译我的项目并部署它时,我得到了一个 Only c
我有一个 WifiScanning 类: public class WifiScanning extends AbstractSetting { /** * */ private static f
我尝试使用 JAXB 将 XML(String) 解析为对象,但我找不到调用我创建的方法的方法,因为我不知道如何指定类 BasicRequest与 SomeRequest如G public class
我在 Spring 中有一个 bean 定义,它是代理对应物,可以在任何地方使用: someInterceptor 一切正常;在
在我的测试中,我有以下行: when(client.runTask(anyString(), anyString(), isA(Iterable.class)).thenReturn(...) isA
我一直在与 Java 泛型作斗争,这就是我正在尝试做的事情: ... sortedActions = new ArrayList(); ActionGroup actionGroup = null;
我想通过 method.invoke(clazz, varargs) 使用反射来使用不同数量的参数进行方法调用,并通过这种方式实现仅一次调用即可调用不同的方法,而不是通过显式的、硬编码的参数数量。目前
我希望能够检查 session 是否包含给定类/标识符的实体。我目前看不到执行此操作的方法。 contains() 接受一个实体对象而不是类+键 get() 如果实体不存在则查询数据库,这是我不想做的
我正在尝试在我的 JBoss EAP 服务器容器中运行 Arquillian 测试。当我运行它们时,我收到以下信息消息: 16:49:48,648 INFO [org.jboss.weld.Clas
这看起来很简单,但我很难找到答案。我在一个类上使用 getDeclaredFields() ,对于某些字段,它返回 Set 而不是集合中的 Class。 for(Field f: clazz.
我有一个接受泛型的类 public class Publisher { ... } 扩展 Storable 的类的对象可以被“发布”(由此类处理)。 在构造函数中,我还获得了有关此实例如果 P
我是一名优秀的程序员,十分优秀!