- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
Spring中Guice的Provider等价于什么?
这是我需要用 Spring 替换的 Guice 代码:
public class MyProxyProvider implements Provider<MyProxy> {
@Inject
Config config;
@Override
public MyProxy get() {
return new MyProxy(httpsclient, config.server, config.user, config.password, config.version);
}
}
这里定义了绑定(bind):
public class MyModule implements Module {
@Override
public void configure(Binder g) {
g.bind(MyProxy.class).toProvider(MyProxyProvider.class);
}
}
最后,我的目标是对代理对象使用@Autowired,如下所示:
public class ConnectionTest {
@Autowired
MyProxy proxy;
}
另请注意,外部 jar 文件中的 MyProxy 类无法修改。
最佳答案
相当于 guice 的 Provider
是 FactoryBean
在 Spring 。
来自文档:
The FactoryBean interface is a point of pluggability into the Spring IoC container's instantiation logic. If you have complex initialization code that is better expressed in Java as opposed to a (potentially) verbose amount of XML, you can create your own FactoryBean, write the complex initialization inside that class, and then plug your custom FactoryBean into the container.
示例
public class MyFactoryBean implements FactoryBean<MyClassInterface> {
@Override
public MyClassInterface getObject() throws Exception {
return new MyClassImplementation();
}
@Override
public Class<?> getObjectType() {
return MyClassInterface.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
当您有外部库和复杂的对象创建时,这是一种很好的方法,可以避免使用冗长的 XML 配置来设置 bean。
为了在您的上下文中使用它,您可以在上下文 XML 中将其作为普通 bean 提供,例如:
...
<bean id="myClass" class="foo.bar.MyFactoryBean" />
...
但是,如果您的 bean 很容易实例化(没有很多依赖项),您可以直接在上下文 XML 上设置它,例如:
<bean id="myClass" class="foo.bar.MyClassImplementation" />
备选
如果您使用 Spring 3,您可以编写一个配置类(用 @Configuration
注释的类),如文档所述here .使用这种方法,您可以执行以下操作:
@Configuration
public class MyConfiguration {
@Bean
public MyClassInterface getMyClass() {
return new MyClassImplementation();
}
}
这会将您的实例添加到 spring 上下文中,并让它在其他类中 Autowiring 。但是需要进行一些配置,按照提供的文档进行操作非常容易。
关于java - Spring中Guice Provider的等价性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16433623/
是: x -= y; 相当于: x = x - y; 最佳答案 不,它们并不等同于您表达它们的方式。 short x = 0, y = 0; x -= y; // This compiles f
这个问题在这里已经有了答案: What is the rationale for all comparisons returning false for IEEE754 NaN values? (1
我在哪里可以找到与 Python maketrans 和 translate 等效的 C# 代码?谢谢! 最佳答案 这应该带你到那里的大部分方式: public class MakeTrans {
我正在 SwiftUI 中构建一个应用程序其中包含很多组件,包括 Text包含长字符串的 View 。 当我在 iPhone 11 上启动该应用程序时,一切正常,但当我在较小的设备(例如 iPhone
这个问题已经有答案了: What is the equivalent lambda expression for System.out::println (2 个回答) Function pointe
我最近在使用 postgres,我必须做一些计算。然而我一直没能模仿Excel的HOUR()函数,我看了official information但这对我帮助不大。 函数接收一个小数,并得到小数的时、分
如果在 cython 中定义了一个指针 vector ,那么与 python 中的 enumerate 类似的函数或过程是什么,用于遍历指针数组中元素的索引和值在 C 声明类型函数内的循环中? 测试.
要选择: select user_id, max(case when value > 0 then timestamp else 0 end) as max_timestamp_whe
如果没有例子,这个问题很难问,所以这里是: #include struct O { }; struct C { template void function1(void (C::*call
我得到了这个结构的实现: struct NodoQ { Etype elem; NodoQ *sig; }; 下面是这段代码吗, typedef NodoQ *PtrNodoQ; PtrNod
我有一些宏需要访问当前类的类型,目前我通过违反 DRY 的模式解决了这个问题: struct ThisScruct{ int a; double b; //example st
我想知道 TensorFlow 的 softmax_cross_entropy_with_logits 是否有等效的 PyTorch 损失函数? 最佳答案 is there an equivalent
我找到了一个 trie 的 java 实现,并希望在 J2ME 中有一个类似的实现。这是代码。 节点类 import java.util.Collections; import java.util.L
我刚刚学习了 GraphQL,我想找到用户 id=2 OR 用户 id=3 现在我将如何进行 GraphQL 查询,我正在使用以下查询获取整个集合 { users() {
假设我有两个 Web 服务:A 和 B。两者都在 Apache 上运行。我希望它们可以从我的主机的不同端口访问:A 来自端口 88,B 来自端口 89。 我可以手动完成(首先创建图像,然后使用“doc
我一直在 excel 中使用一个非常简单的数组公式来处理一些数据集,但是它们变得太大并且在我更新计算时完全破坏了我的计算机性能。 excel表格和MySQL数据库布局如下: +-Timestamp-+
我有一个类,其实例要通过不同于它们携带的数据值的标识来区分。在我的代码中,我打算使用 == 来表示两个实例在它们的数据方面是等价的,并且 is 表示两个变量引用同一个实例,也就是说,他们是相同的。根据
我正在 Windows 中使用 WinSock 2.0 开发代理服务器。如果我想在阻塞模型中开发它,select() 是等待客户端或远程服务器从中接收数据的方法。是否有任何适用的方法可以使用 I/O
我正在将我制作的 Android 应用移植到 iOS。 Android 有一个 Yield() 函数可以将线程从运行中移到线程队列的后面(?)。这很有用,这样该线程就不会占用过多的 CPU 并使其他一
这是否保证始终为真: std::numeric_limits::max() == INT_MAX C++ 标准对此有何规定?我在标准中找不到任何明确说明这一点的引用资料,但我一直在阅读这些内容应该是等
我是一名优秀的程序员,十分优秀!