set resource in spring configuration file

你。 提交于 2019-12-05 18:06:41

You need to use a ResourcePatternResolver to translate classpath*:dozer/**/*.dzr.xml into a Resource[]. There are 2 main options you can use.

  1. Inject the ApplicationContext into your configuration class, cast it to a ResourcePatternResolver and use the getResources method. Al Spring default application context implementations implement the ResourcePatternResolver interface.
  2. Create a PathMatchingResourcePatternResolver with or without the earlier mentioned context.
  3. Use the ResourcePatternUtils with an injected ResourceLoader.

Use the ResourcePatternUtils

@Configuration
public class MyConfiguration {

    @Autowired
    private ResourceLoader resourceLoader;

    public DozerBeanMapperFactoryBean mapper() throws IOException {
        DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
        // ResourceLoader is allowed to be null when using the ResourceLoaderUtils.
        ResourcePatternResolver resolver = ResourceLoaderUtils.getResourcePatternResolver(resourceLoader);
        Resource[] mappingFiles = resolver.getResources("classpath*:dozer/**/*.dzr.xml");
        mapper.setMappingFiles(mappingFiles);
        return mapper;
    }
}

The advantages of this last approach is that you aren't tied to the PathMatchingResourcePatternResolver but just the interface. The utility class determines, based on the injected ResourceLoader what it does. One should prefer this way of loading resources.

Using ApplicationContext

@Configuration
public class MyConfiguration {

    @Autowired
    private ApplicationContext context;

    public DozerBeanMapperFactoryBean mapper() throws IOException {
        DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
        Resource[] mappingFiles = ((ResourcePatternResolver) context).getResources("classpath*:dozer/**/*.dzr.xml");
        mapper.setMappingFiles(mappingFiles);
        return mapper;
    }
}

Using PathMatchingResourcePatternResolver

@Configuration
public class MyConfiguration {

    private PathMatchingPatternResolver resolver = new PathMatchingPatternResolver();

    public DozerBeanMapperFactoryBean mapper() throws IOException {
        DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
        Resource[] mappingFiles = resolver.getResources("classpath*:dozer/**/*.dzr.xml");
        mapper.setMappingFiles(mappingFiles);
        return mapper;
    }
}

Or if you want to reuse the already existing ResourceLoader a slighty different version:

@Configuration
public class MyConfiguration {

    @Autowired
    private ResourceLoader resourceLoader;

    public DozerBeanMapperFactoryBean mapper() throws IOException {
        DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
        Resource[] mappingFiles = new PathMatchingPatternResolver(resourceLoader).getResources("classpath*:dozer/**/*.dzr.xml");
        mapper.setMappingFiles(mappingFiles);
        return mapper;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!