我正在编写一个应用程序,该应用程序在Web层中使用Springboot/Java 8并在工作线程层中运行Java应用程序。工作线程从消息队列中读取。我有在两个层中使用的通用服务单例。我想使用 javax.inject.Provider 模式将本地原型 bean 注入到公共服务实例中,而不是将它们传递到所有服务方法中。所以我试图弄清楚如何最好地创建javax.inject.Provider<ServiceContextInfo>
.
我在配置文件中尝试了这个:
@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;
}
然后在工作线程的消息接收器中,这被称为:
ProviderCreatingFactoryBean providerFactoryBean =
applicationContext.getBean(ProviderCreatingFactoryBean.class);
Provider<Object> serviceContextInfo = providerFactoryBean.getObject();
((ServiceContextInfo)serviceContextInfo.get()).
setDynamicData(webToWorkerMessage.getDynamicData());
下面是抽象服务类:
public class AbstractService {
@Inject
protected Provider<ServiceContextInfo> contextInfo;
}
下面是 ServiceContextInfo 类:
@Data
@Scope("prototype")
@Component
public class ServiceContextInfo {
private ReportInfo reportInfo;
private String dynamicData;
}
我觉得我走在正确的轨道上;但我只是没有在使用XML,Java配置的简单示例以及如何最好地支持动态数据需求之间建立关联......消息侦听器中对工厂 Bean 的调用似乎不起作用。另外,我宁愿不分配给Provider<Object>
,而是分配给Provider<ServiceContextInfo>
。
在 Web 层上,我还将构建 ServiceContextInfo 对象,以便服务类对两个层执行相同的操作。
协助将不胜感激! 提前感谢...
毕竟我能够让它工作。仍然必须处理辅助角色层中的Provider<Object>
问题。Web 层根据需要注入它。其余的都很好用。这并不像我想象的那么困难...