问题
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;
}
回答1:
You need to use a ResourcePatternResolver to translate classpath*:dozer/**/*.dzr.xml into a Resource[]. There are 2 main options you can use.
- Inject the
ApplicationContextinto your configuration class, cast it to aResourcePatternResolverand use thegetResourcesmethod. Al Spring default application context implementations implement theResourcePatternResolverinterface. - Create a
PathMatchingResourcePatternResolverwith or without the earlier mentioned context. - 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;
}
}
来源:https://stackoverflow.com/questions/24403765/set-resource-in-spring-configuration-file