在spring配置文件中设置资源 [英] set resource in spring configuration file

查看:140
本文介绍了在spring配置文件中设置资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在春季配置中配置推土机。当使用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 to

DozerBeanMapperFactoryBean 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 ---

@Bean
public DozerBeanMapperFactoryBean configDozer() throws IOException {
    DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
    Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:dozer/**/*.dzr.xml");
    mapper.setMappingFiles(resources);
    return mapper;
}


推荐答案

您需要使用< a href = http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/support/ResourcePatternResolver.html rel = noreferrer> 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.


  1. ApplicationContext 注入配置类,将其强制转换为 ResourcePatternResolver 并使用 getResources 方法。 Spring的默认应用程序上下文实现实现了 ResourcePatternResolver 接口。

  2. 创建 PathMatchingResourcePatternResolver 带有或不带有前面提到的上下文。

  3. 使用 ResourcePatternUtils 并注入 ResourceLoader

  1. 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.
  2. Create a PathMatchingResourcePatternResolver with or without the earlier mentioned context.
  3. Use the ResourcePatternUtils with an injected 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 确定其作用。人们应该更喜欢这种加载资源的方式。

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

@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 略有不同:

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

这篇关于在spring配置文件中设置资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆