【问题标题】:set resource in spring configuration file在spring配置文件中设置资源
【发布时间】:2014-08-15 17:26:49
【问题描述】:

我正在尝试在 spring 配置中配置推土机。使用 xml 配置时会像

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

【问题讨论】:

  • 注入ResourceLoaderApplicationContext。用它构造一个PathMatchingResourcePatternResolver(你也可以尝试在没有上下文或资源加载器的情况下创建),并在你的模式中使用getResources方法来获取资源。
  • 我不会抛出异常,如果在加载您可能不想启动应用程序的资源时出现问题,只需添加 throws IOException
  • 谢谢。好点:)
  • 修改后的答案也反映了这一点。

标签: spring configuration resources classpath spring-annotations


【解决方案1】:

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

  1. ApplicationContext 注入您的配置类,将其转换为ResourcePatternResolver 并使用getResources 方法。 Al Spring 默认应用程序上下文实现实现了ResourcePatternResolver 接口。
  2. 创建一个PathMatchingResourcePatternResolver,无论是否包含前面提到的上下文。
  3. 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;
    }
}

【讨论】:

  • 谢谢。使用 PathMatchingPatternResolver 解决了我的问题。自动装配在配置中不起作用(可能是因为它们尚未创建!)我会更新我的问题代码。
  • Autowired 像普通组件一样在配置类中工作。但是没有PathMatchingPatternResolver,因此您必须自己构建(这也是我在代码 sn-ps 中显示的内容)。注入ResourceLoaderApplicationContext 应该可以工作。
  • ResourceLoader 或 `ApplicationContext' 的注入导致空指针异常。它们在应用程序上下文初始化时为空
  • 这应该可以,因为我自己已经多次使用过它。您的设置中一定有一些奇怪/错误的地方。
  • 无法自动连接applicationContext 是我的首要问题。我在哪里可以放置我的配置,以便您可以看到原因将其添加为此处的答案是不正确的。它工作没有任何问题。只是想找出问题出在哪里
猜你喜欢
  • 1970-01-01
  • 2020-08-23
  • 2018-06-20
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
  • 2011-10-12
相关资源
最近更新 更多