简体   繁体   English

如何创建javax.inject.Provider <MyObject> 使用Spring

[英]How to create javax.inject.Provider<MyObject> Using Spring

I am writing an app that uses Springboot / Java 8 in the web tier and runs a Java Application in a worker tier. 我正在编写一个在Web层中使用Springboot / Java 8并在工作层中运行Java应用程序的应用程序。 The worker reads from a message queue. 工作程序从消息队列中读取。 I have common service singletons that are used in both tiers. 我在两个层中都有通用的服务单例。 I would like to use the javax.inject.Provider pattern to inject local prototype beans into the common service instances rather than passing them throughout all the service methods. 我想使用javax.inject.Provider模式将本地原型bean注入到通用服务实例中,而不是将它们传递给所有服务方法。 So I'm trying to figure out how to best create javax.inject.Provider<ServiceContextInfo> . 所以我试图找出如何最好地创建javax.inject.Provider<ServiceContextInfo>

I tried this in the config file: 我在配置文件中尝试了这个:

@Configuration
public class MyConfiguration {

    @Bean 
    ReportInfo ReportInfo() {
        return new ReportInfo();
    }

    @Bean(name = "serviceContextInfo")
    public ServiceContextInfo serviceProviderInfoBean() {
        ServiceContextInfo info = new ServiceContextInfo();
        info.setReportInfo(reportInfo());
        return info;
    }

    @Bean
    public ProviderCreatingFactoryBean providerCreatingFactoryBean() {
        ServiceContextInfo info = new ServiceContextInfo();
        info.setReportInfo(reportInfo());
        ProviderCreatingFactoryBean providerFactory = new ProviderCreatingFactoryBean();
        providerFactory.setTargetBeanName("serviceContextInfo");
        return providerFactory;
    }

Then in the message receiver of the worker this is called: 然后在工作程序的消息接收器中将其称为:

    ProviderCreatingFactoryBean providerFactoryBean = 
                        applicationContext.getBean(ProviderCreatingFactoryBean.class);
    Provider<Object> serviceContextInfo = providerFactoryBean.getObject();
    ((ServiceContextInfo)serviceContextInfo.get()).
            setDynamicData(webToWorkerMessage.getDynamicData());

Here is the abstractService class: 这是abstractService类:

public class AbstractService {

     @Inject
     protected Provider<ServiceContextInfo> contextInfo;
}

Here is the ServiceContextInfo class: 这是ServiceContextInfo类:

@Data
@Scope("prototype")
@Component
public class ServiceContextInfo {

    private ReportInfo reportInfo;
    private String dynamicData;

}

I feel like I'm on the right track; 我觉得自己在正确的轨道上; but I'm just not making the correlation between the simple examples using XML, Java config, and how to best support the dynamic data requirement... The call to the factory bean in the message listener doesn't appear to be working. 但是我只是没有在使用XML,Java配置的简单示例与如何最好地支持动态数据需求之间建立关联。在消息侦听器中对工厂bean的调用似乎无效。 Also, I would rather not assign to Provider<Object> , but rather to Provider<ServiceContextInfo> . 另外,我宁愿不分配给Provider<Object> ,而是赋给Provider<ServiceContextInfo>

On the web tier, I would also build the ServiceContextInfo objects so that the service classes perform the same for both tiers. 在Web层上,我还将构建ServiceContextInfo对象,以便服务类对这两个层执行相同的操作。

Assistance would be greatly appreciated! 协助将不胜感激! Thanks in advance... 提前致谢...

I was able to get this to work after all. 毕竟,我能够使它正常工作。 Still have to deal with the Provider<Object> issue in the worker tier. 仍然必须处理工作层中的Provider<Object>问题。 The web tier injects it typed as desired. Web层将根据需要注入其键入的内容。 The rest works nicely. 其余的很好。 It was not as difficult as I thought... 这并不像我想的那么难...

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

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