简体   繁体   English

在spring配置文件中设置资源

[英]set resource in spring configuration file

i am trying to config dozer in spring configuration. 我想在spring配置中配置推土机。 when using xml config it would be like 当使用xml配置时,它就像

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

how can i define resources in config file. 如何在配置文件中定义资源。 i tried using ctx.getResource() but i cannot access to ApplicationContext in Configuration class. 我尝试使用ctx.getResource()但我无法访问Configuration类中的ApplicationContext。

i tried ContextRefreshedEvent and add resources from there, but still no luck. 我尝试了ContextRefreshedEvent并从那里添加资源,但仍然没有运气。 (afterPropertiesSet is already called and added mappings wont work) (已调用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();
    }
}
}

also tried to use ClassPathResource but can't find the correct way to 还尝试使用ClassPathResource但无法找到正确的方法

DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
mapper.setMappingFiles(new Resource[]{new ClassPathResource("classpath*:dozer/**/*.dzr.xml")});
return mapper;

how can i add ClassPathResource as mapping locations? 如何将ClassPathResource添加为映射位置?

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

You need to use a ResourcePatternResolver to translate classpath*:dozer/**/*.dzr.xml into a Resource[] . 您需要使用ResourcePatternResolverclasspath*:dozer/**/*.dzr.xml转换为Resource[] There are 2 main options you can use. 您可以使用2种主要选项。

  1. Inject the ApplicationContext into your configuration class, cast it to a ResourcePatternResolver and use the getResources method. ApplicationContext注入配置类,将其getResources转换为ResourcePatternResolver并使用getResources方法。 Al Spring default application context implementations implement the ResourcePatternResolver interface. Al Spring默认应用程序上下文实现实现ResourcePatternResolver接口。
  2. Create a PathMatchingResourcePatternResolver with or without the earlier mentioned context. 使用或不使用前面提到的上下文创建PathMatchingResourcePatternResolver
  3. Use the ResourcePatternUtils with an injected ResourceLoader . 使用ResourcePatternUtils与注入ResourceLoader

Use the ResourcePatternUtils 使用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;
    }
}

The advantages of this last approach is that you aren't tied to the PathMatchingResourcePatternResolver but just the interface. 最后一种方法的优点是您不依赖于PathMatchingResourcePatternResolver而只是接口。 The utility class determines, based on the injected ResourceLoader what it does. 实用程序类根据注入的ResourceLoader确定它的作用。 One should prefer this way of loading resources. 人们应该更喜欢这种加载资源的方式。

Using ApplicationContext 使用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;
    }
}

Using PathMatchingResourcePatternResolver 使用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;
    }
}

Or if you want to reuse the already existing ResourceLoader a slighty different version: 或者,如果您想重用已经存在的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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM