本文介绍了在spring配置文件中设置资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试在春季配置中配置推土机。当使用xml配置时,会像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>我如何在配置文件中定义资源。我尝试使用 ctx.getResource(),但无法访问Configuration类中的ApplicationContext。how can i define resources in config file. i tried using ctx.getResource() but i cannot access to ApplicationContext in Configuration class.我尝试了ContextRefreshedEvent和从那里添加资源,但仍然没有运气。 (afterPropertiesSet已被调用,添加的映射将无法工作)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(); }}}也尝试使用ClassPathResource,但是找不到正确的方法also tried to use ClassPathResource but can't find the correct way toDozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();mapper.setMappingFiles(new Resource[]{new ClassPathResource("classpath*:dozer/**/*.dzr.xml")});return mapper;我如何添加ClassPathResource作为映射位置?how can i add ClassPathResource as mapping locations? --- ANSWER ---@Beanpublic 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个主要选项。You need to use a ResourcePatternResolver to translate classpath*:dozer/**/*.dzr.xml into a Resource[]. There are 2 main options you can use. 将 ApplicationContext 注入配置类,将其强制转换为 ResourcePatternResolver 并使用 getResources 方法。 Spring的默认应用程序上下文实现实现了 ResourcePatternResolver 接口。 创建 PathMatchingResourcePatternResolver 带有或不带有前面提到的上下文。 使用 ResourcePatternUtils 并注入 ResourceLoader 。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.Create a PathMatchingResourcePatternResolver with or without the earlier mentioned context.Use the ResourcePatternUtils with an injected ResourceLoader. 使用ResourcePatternUtils@Configurationpublic 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 确定其作用。人们应该更喜欢这种加载资源的方式。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. 使用ApplicationContext@Configurationpublic 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@Configurationpublic 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 略有不同:Or if you want to reuse the already existing ResourceLoader a slighty different version:@Configurationpublic 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; }} 这篇关于在spring配置文件中设置资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!