向Spring bean注入依赖 [英] Injecting dependency to a Spring bean
问题描述
我想向Spring bean注入单例对象依赖项。问题是我无法访问和修改要注入其对象的类。让我在示例上进行描述。
I would like to inject a singleton object dependency to a Spring bean. The catch is that I can't access and modify the class whose object I want to be injected. Let me describe on the example.
所以我有我的界面以及该界面的实现,如下所示。
So I have my interface, and the implementation of this interface, like the following.
public interface MyServiceProxy {
String BEAN_NAME = "MyServiceProxy";
Data getData(String dataId);
}
public class MyServiceProxyImpl implements MyServiceProxy {
private final MyServiceClient client;
public MyServiceProxyImpl(MyServiceClient client) {
this.client = client;
}
@Override
public Data getData(String dataId) {//...}
然后在我的Configuration类中,我正在创建一个bean,但是需要在构造函数中将其传递给 MyServiceClient
对象,而且要注意的是,我不能使 MyServiceClient
成为一个bean,因为它来自外部程序包,我无法对其进行修改。
Then in my Configuration class, I am creating a bean, but I need to pass it the MyServiceClient
object in the constructor, and the catch is that I can't make MyServiceClient
a bean because it's from external package and I can't modify it.
@Configuration
public class MyServiceProxyConfiguration {
@Bean(name = MyServiceProxy.BEAN_NAME)
public MyServiceProxy getMyServiceProxy(MyServiceClient client) { // could not autowire client
return new MyServiceProxyImpl(client);
}
}
所以我想做的是将参数传递/自动装配到 getMyServiceProxy
bean。当前IntelliJ给我一个错误无法自动连接客户端。如何实现?
So what I would like to do, is being able to pass/autowire an argument to getMyServiceProxy
bean. Currently IntelliJ is giving me an error Could not autowire client. How can this be achieved?
更新
是否需要以下工作? ?因为IntelliJ仍在报告无法自动装配错误。因此,如果我创建了一个bean方法,该方法返回要注入的 client
客户端,然后将@Inject注释添加到要注入的方法中。
Would something like the following work? Because IntelliJ is still reporting an "could not autowire" error. So if I created a bean method that returns the client
I want injected, and then add @Inject annotation to the method where I want it injected.
public class MyServiceClientBuilder {
private final ClientBuilder builder;
public MyServiceClientBuilder(ClientBuilder builder) {
this.builder = builder;
}
@Bean
public MyServiceClient build() {
return builder.newClient();
}
@Configuration
public class MyServiceProxyConfiguration {
@Inject
@Bean(name = MyServiceProxy.BEAN_NAME)
public MyServiceProxy getMyServiceProxy(MyServiceClient client) { // could not autowire client
return new MyServiceProxyImpl(client);
}
}
推荐答案
您可以像这样在配置文件中将 MyServiceClient
定义为单独的bean:
You can define MyServiceClient
as a separate bean in your configuration file like this:
@Configuration
public class MyServiceProxyConfiguration {
@Bean
public MyServiceClient getMyServiceClient () {
return MyServiceClient.getInstance(); //initiate MyServiceClient
}
@Bean(name = MyServiceProxy.BEAN_NAME)
public MyServiceProxy getMyServiceProxy(MyServiceClient client) {
return new MyServiceProxyImpl(client);
}
}
我尚未测试此代码,但它应该可以工作
I have not tested this code, but it should work.
这篇关于向Spring bean注入依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!