简体   繁体   中英

set resource in spring configuration file

i am trying to config dozer in spring configuration. when using xml config it would be like

<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
    <property name="mappingFiles" value="classpath*:dozer/**/*.dzr.xml"/>
</bean>

how can i define resources in config file. i tried using ctx.getResource() but i cannot access to ApplicationContext in Configuration class.

i tried ContextRefreshedEvent and add resources from there, but still no luck. (afterPropertiesSet is already called and added mappings wont work)

public class ContextRefreshedEventBuilder extends ContextRefreshedEvent {
public ContextRefreshedEventBuilder(ApplicationContext ctx) {
    super(ctx);
    DozerBeanMapperFactoryBean mapper = ctx.getBean(DozerBeanMapperFactoryBean.class);
    try {
        mapper.setMappingFiles(ctx.getResources("classpath*:dozer/**/*.dzr.xml"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

also tried to use ClassPathResource but can't find the correct way to

DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
mapper.setMappingFiles(new Resource[]{new ClassPathResource("classpath*:dozer/**/*.dzr.xml")});
return mapper;

how can i add ClassPathResource as mapping locations?

---ANSWER---

@Bean
public DozerBeanMapperFactoryBean configDozer() throws IOException {
    DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
    Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:dozer/**/*.dzr.xml");
    mapper.setMappingFiles(resources);
    return mapper;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM