存在依赖于每个文件创建 spring bean 的变体
Variations of creating spring bean dependant on each file exists
我有 'reporting' 服务,需要输入元数据,此元数据是从 metadata.xml
或 metadata.properties
.
加载的
现在,来自这些文件的输入流被创建为 spring bean 并在服务器中自动装配。但是,如果用户不提供元数据文件,服务不报告任何内容,如果用户只提供 1 个文件,它从该文件中获取数据,如果用户提供两个文件,服务的内部逻辑将决定它取哪一个(对我的问题不重要)。
到目前为止,我是这样创建输入流的:
@Bean(name = "xml_bean")
@ConditionalOnResource(resources = "classpath:metadata.xml")
@XmlFile
publihgc InputStream getXmlFileInputStream() {
return getClass().getClassLoader().getResourceAsStream(XML_FILE_NAME);
}
@Bean
@XmlFile
@ConditionalOnMissingBean(name = "xml_bean")
public InputStream getXmlFileDefault() {
//empty input stream
return new InputStream() {
@Override
public int read() {
return 0;
}
};
}
@Bean(name = "properties_bean")
@ConditionalOnResource(resources = "classpath:metadata.properties")
@PropertyFile
public InputStream getPropertiesFileInputStream() {
return getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE_NAME);
}
@Bean
@PropertyFile
@ConditionalOnMissingBean(name = "properties_bean")
public InputStream getPropertiesFileInputStreamDefault() {
//empty input stream
return new InputStream() {
@Override
public int read() {
return 0;
}
};
}
Spring 检查文件是否存在,如果存在则创建输入流 bean,否则创建空流 bean。
现在服务,正如我所说的,我有 4 个案例:
- 两个文件都已定义
- 没有定义文件
- XML 文件仅定义
- 只定义属性文件
这是 4 个案例,到目前为止我已经创建了 4 个条件 bean:
1)两个文件都定义了:
@Bean("reportingService")
@ConditionalOnBean(name = {"xml_bean", "properties_bean"})
public ReportingService reportingService(@XmlFille final InputStream xmlFileInputStream,
@PropertyFile final InputStream propertiesFileInputStream) {
return new CachedReportingService (xmlFileInputStream, propertiesFileInputStream);
}
没有定义文件
@Bean
@ConditionalOnMissingBean(ReportingService.class)
public ReportingService reportingServiceDefault() {
return new NoOpReportingService ();
}
只定义了 xml 个文件
@Bean
@ConditionalOnBean(name = {"xml_bean"})
@ConditionalOnMissingBean(name = "reportingService")
public ReportingService reportingServiceXML (@XmlFile final InputStream xmlFileInputStream) {
return new CachedReportingService (xmlFileInputStream, null);
}
只定义了属性文件
@Bean
@ConditionalOnBean(name = {"bter_prop_bean"})
@ConditionalOnMissingBean(name = "reportingService")
public ReportingService reportingServiceProperties(
@BterProperties final InputStream propertiesFileInputStream) {
return new CachedReportingService (null, propertiesFileInputStream);
}
现在这可行了,但是对于每种情况,我都需要创建另一个 bean 定义。如果我有 3 个这样的文件,案例将从 4 跳到 8。没有更好的方法吗?
当我尝试做类似的事情时:
@Bean(name = "xml_bean")
@XmlFile
publihgc InputStream getXmlFileInputStream() {
return getClass().getClassLoader().getResourceAsStream(XML_FILE_NAME);
}
@Bean
public ReportingService reportingService(@XmlFille final InputStream xmlFileInputStream,
@PropertyFile final InputStream propertiesFileInputStream) {
return new CachedReportingService (xmlFileInputStream, propertiesFileInputStream);
}
但这会失败,就好像当 xml_bean 没有文件时,classLoader 导致 null,并抛出错误,因为 @XmlFille final InputStream xmlFileInputStream
没有自动装配的候选者。
像这样创建bean有什么技巧吗?我使用的方式似乎很冗余和复杂
您可以用一种方法完成所有这些。您可以使那些 InputStream
参数 Optional
并检查可用的内容,基于 return CachedReportingService
或 NoOpReportingService
。
@Bean
public ReportingService reportingService(@XmlFille Optional<InputStream> xmlFileInputStream,
@PropertyFile Optional<InputStream> propertiesFileInputStream) {
if (xmlFileInputStream.isPresent() || propertiesFileInputStream.isPresent()) {
return new CachedReportingService(xmlFileInputStream.orElse(null), propertiesFileInputStream.orElse(null));
} else {
return new NoOpReportingService();
}
}
类似的东西。不是所有的事情都需要用注解来解决,你Java触手可及,好好利用它。
我有 'reporting' 服务,需要输入元数据,此元数据是从 metadata.xml
或 metadata.properties
.
现在,来自这些文件的输入流被创建为 spring bean 并在服务器中自动装配。但是,如果用户不提供元数据文件,服务不报告任何内容,如果用户只提供 1 个文件,它从该文件中获取数据,如果用户提供两个文件,服务的内部逻辑将决定它取哪一个(对我的问题不重要)。
到目前为止,我是这样创建输入流的:
@Bean(name = "xml_bean")
@ConditionalOnResource(resources = "classpath:metadata.xml")
@XmlFile
publihgc InputStream getXmlFileInputStream() {
return getClass().getClassLoader().getResourceAsStream(XML_FILE_NAME);
}
@Bean
@XmlFile
@ConditionalOnMissingBean(name = "xml_bean")
public InputStream getXmlFileDefault() {
//empty input stream
return new InputStream() {
@Override
public int read() {
return 0;
}
};
}
@Bean(name = "properties_bean")
@ConditionalOnResource(resources = "classpath:metadata.properties")
@PropertyFile
public InputStream getPropertiesFileInputStream() {
return getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE_NAME);
}
@Bean
@PropertyFile
@ConditionalOnMissingBean(name = "properties_bean")
public InputStream getPropertiesFileInputStreamDefault() {
//empty input stream
return new InputStream() {
@Override
public int read() {
return 0;
}
};
}
Spring 检查文件是否存在,如果存在则创建输入流 bean,否则创建空流 bean。
现在服务,正如我所说的,我有 4 个案例:
- 两个文件都已定义
- 没有定义文件
- XML 文件仅定义
- 只定义属性文件
这是 4 个案例,到目前为止我已经创建了 4 个条件 bean:
1)两个文件都定义了:
@Bean("reportingService")
@ConditionalOnBean(name = {"xml_bean", "properties_bean"})
public ReportingService reportingService(@XmlFille final InputStream xmlFileInputStream,
@PropertyFile final InputStream propertiesFileInputStream) {
return new CachedReportingService (xmlFileInputStream, propertiesFileInputStream);
}
没有定义文件
@Bean
@ConditionalOnMissingBean(ReportingService.class)
public ReportingService reportingServiceDefault() {
return new NoOpReportingService ();
}
只定义了 xml 个文件
@Bean
@ConditionalOnBean(name = {"xml_bean"})
@ConditionalOnMissingBean(name = "reportingService")
public ReportingService reportingServiceXML (@XmlFile final InputStream xmlFileInputStream) {
return new CachedReportingService (xmlFileInputStream, null);
}
只定义了属性文件
@Bean
@ConditionalOnBean(name = {"bter_prop_bean"})
@ConditionalOnMissingBean(name = "reportingService")
public ReportingService reportingServiceProperties(
@BterProperties final InputStream propertiesFileInputStream) {
return new CachedReportingService (null, propertiesFileInputStream);
}
现在这可行了,但是对于每种情况,我都需要创建另一个 bean 定义。如果我有 3 个这样的文件,案例将从 4 跳到 8。没有更好的方法吗?
当我尝试做类似的事情时:
@Bean(name = "xml_bean")
@XmlFile
publihgc InputStream getXmlFileInputStream() {
return getClass().getClassLoader().getResourceAsStream(XML_FILE_NAME);
}
@Bean
public ReportingService reportingService(@XmlFille final InputStream xmlFileInputStream,
@PropertyFile final InputStream propertiesFileInputStream) {
return new CachedReportingService (xmlFileInputStream, propertiesFileInputStream);
}
但这会失败,就好像当 xml_bean 没有文件时,classLoader 导致 null,并抛出错误,因为 @XmlFille final InputStream xmlFileInputStream
没有自动装配的候选者。
像这样创建bean有什么技巧吗?我使用的方式似乎很冗余和复杂
您可以用一种方法完成所有这些。您可以使那些 InputStream
参数 Optional
并检查可用的内容,基于 return CachedReportingService
或 NoOpReportingService
。
@Bean
public ReportingService reportingService(@XmlFille Optional<InputStream> xmlFileInputStream,
@PropertyFile Optional<InputStream> propertiesFileInputStream) {
if (xmlFileInputStream.isPresent() || propertiesFileInputStream.isPresent()) {
return new CachedReportingService(xmlFileInputStream.orElse(null), propertiesFileInputStream.orElse(null));
} else {
return new NoOpReportingService();
}
}
类似的东西。不是所有的事情都需要用注解来解决,你Java触手可及,好好利用它。