我正在尝试在春季配置中配置推土机。当使用xml config时,就像
<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
<property name="mappingFiles" value="classpath*:dozer/**/*.dzr.xml"/>
</bean>
我如何在配置文件中定义资源。我尝试使用,ctx.getResource()
但无法访问Configuration类中的ApplicationContext。
我尝试了ContextRefreshedEvent并从那里添加资源,但是还是没有运气。(afterPropertiesSet已经被调用,添加的映射将无法工作)
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();
}
}
}
也尝试使用ClassPathResource,但找不到正确的方法
DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
mapper.setMappingFiles(new Resource[]{new ClassPathResource("classpath*:dozer/**/*.dzr.xml")});
return mapper;
如何添加ClassPathResource作为映射位置?
- -回答 - -
@Bean
public DozerBeanMapperFactoryBean configDozer() throws IOException {
DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:dozer/**/*.dzr.xml");
mapper.setMappingFiles(resources);
return mapper;
}
您需要使用ResourcePatternResolver
将转换classpath*:dozer/**/*.dzr.xml
为Resource[]
。您可以使用2个主要选项。
ApplicationContext
注入您的配置类,将其强制转换为,ResourcePatternResolver
然后使用getResources
方法。Spring的默认应用程序上下文实现实现了该ResourcePatternResolver
接口。PathMatchingResourcePatternResolver
带有或不带有前面提到的上下文的。ResourcePatternUtils
一起使用ResourceLoader
。使用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;
}
}
最后一种方法的优点是,您不必PathMatchingResourcePatternResolver
局限于接口,而只需绑定接口即可。实用程序类根据注入的ResourceLoader
内容确定其作用。人们应该更喜欢这种加载资源的方式。
使用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;
}
}
使用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;
}
}
或者,如果您想重用已经存在ResourceLoader
的略有不同的版本:
@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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句