gpt4 book ai didi

spring - 在spring配置文件中设置资源

转载 作者:行者123 更新时间:2023-12-02 06:45:53 26 4
gpt4 key购买 nike

我正在尝试在 Spring 配置中配置推土机。当使用 xml 配置时,它会像

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

如何在配置文件中定义资源。我尝试使用 ctx.getResource() ,但无法访问配置类中的 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;
}

最佳答案

您需要使用ResourcePatternResolverclasspath*:dozer/**/*.dzr.xml 转换为 Resource[]。您可以使用 2 个主要选项。

  1. ApplicationContext 注入(inject)到您的配置类中,将其转换为 ResourcePatternResolver 并使用 getResources 方法。 Al Spring 默认应用程序上下文实现实现了 ResourcePatternResolver 接口(interface)。
  2. 创建一个带有或不带有前面提到的上下文的 PathMatchingResourcePatternResolver
  3. 使用ResourcePatternUtils带有注入(inject)的 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,而只依赖于接口(interface)。实用程序类根据注入(inject)的 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;
}
}

关于spring - 在spring配置文件中设置资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24403765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com