简体   繁体   English

向Spring bean注入依赖

[英]Injecting dependency to a Spring bean

I would like to inject a singleton object dependency to a Spring bean. 我想向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) {//...}

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类中,我正在创建一个Bean,但是我需要在构造函数中将其传递给MyServiceClient对象,但要注意的是,我不能使MyServiceClient成为一个Bean,因为它来自外部包,并且无法修改它。

@Configuration
public class MyServiceProxyConfiguration {

    @Bean(name = MyServiceProxy.BEAN_NAME)
    public MyServiceProxy getMyServiceProxy(MyServiceClient client) { // could not autowire client
        return new MyServiceProxyImpl(client);
    }
}

So what I would like to do, is being able to pass/autowire an argument to getMyServiceProxy bean. 因此,我想做的是能够将参数传递/自动装配到getMyServiceProxy bean。 Currently IntelliJ is giving me an error Could not autowire client . 当前IntelliJ给我一个错误无法自动连接客户端 How can this be achieved? 如何做到这一点?

UPDATE 更新

Would something like the following work? 可以进行以下工作吗? Because IntelliJ is still reporting an "could not autowire" error. 因为IntelliJ仍在报告“无法自动装配”错误。 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. 因此,如果我创建了一个返回要注入的client的bean方法,然后将@Inject批注添加到要注入的方法中。

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);
    }
}

You can define MyServiceClient as a separate bean in your configuration file like this: 您可以像这样在配置文件中将MyServiceClient定义为单独的bean:

@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. 我尚未测试此代码,但它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM