Java Spring-如何仅通过注释初始化映射
是否可以仅使用注释来初始化映射?我想让我的包及其子包独立,所以我不想在context.xml中定义任何内容。@Configuration完成了这项工作。这是配置类Java Spring-如何仅通过注释初始化映射,java,spring,Java,Spring,是否可以仅使用注释来初始化映射?我想让我的包及其子包独立,所以我不想在context.xml中定义任何内容。@Configuration完成了这项工作。这是配置类 @org.springframework.context.annotation.Configuration public class Configuration { @Autowired private ImportHandler monographImportHandler; @Autowired
@org.springframework.context.annotation.Configuration
public class Configuration {
@Autowired
private ImportHandler monographImportHandler;
@Autowired
private ImportHandler periodicalImportHandler;
@Bean(name = "importHandlerCatalog")
@Scope("prototype")
public Map<MetsFileType, ImportHandler> sipPackageProcessorCatalog() {
Map<MetsFileType, ImportHandler> catalog = new HashMap<>();
catalog.put(MetsFileType.MONOGRAPH, monographImportHandler);
catalog.put(MetsFileType.PERIODICAL, periodicalImportHandler);
return catalog;
}
}
@org.springframework.context.annotation.Configuration
公共类配置{
@自动连线
私人进口商专论经销商;
@自动连线
私人进口商周期性进口商;
@Bean(name=“importHandlerCatalog”)
@范围(“原型”)
公共地图sipPackageProcessorCatalog(){
映射目录=新的HashMap();
目录.放置(MetsFileType.专论,专论处理器);
目录放置(MetsFileType.Periodic、PeriodicLimporthandler);
退货目录;
}
}
这就是我访问它的方式:
@Resource(name = "importHandlerCatalog")
public Map<MetsFileType, ImportHandler> importHandlerCatalog;
@Resource(name=“importHandlerCatalog”)
公共地图导入目录;
以及如何指定映射内容?您可以@Autowire
映射,并在@Configuration
类中声明一个@Bean
方法,返回您选择的映射
。