Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - How to initialize map by annotations only

Tags:

java

spring

Is it possible to initialize Map by using only annotations? I want to have my package and its sub-packages independent, so I don't want to define anything in the context.xml.

like image 298
Michal Krasny Avatar asked Mar 26 '26 05:03

Michal Krasny


1 Answers

@Configuration did the job. This is the configuration class

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

}

And this is how I access it:

@Resource(name = "importHandlerCatalog")
public Map<MetsFileType, ImportHandler> importHandlerCatalog;
like image 61
Michal Krasny Avatar answered Mar 28 '26 18:03

Michal Krasny