在spring配置文件中设置资源

在spring配置文件中设置资源,spring,configuration,resources,classpath,spring-annotations,Spring,Configuration,Resources,Classpath,Spring Annotations,我正在尝试在spring配置中配置推土机。当使用xml配置时,会像 <bean class="org.dozer.spring.DozerBeanMapperFactoryBean"> <property name="mappingFiles" value="classpath*:dozer/**/*.dzr.xml"/> </bean> 还尝试使用ClassPathResource,但找不到正确的方法 DozerBeanMapperFactoryB

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

<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
    <property name="mappingFiles" value="classpath*:dozer/**/*.dzr.xml"/>
</bean>
还尝试使用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;
}
您需要使用将
类路径*:dozer/***/*.dzr.xml
转换为
资源[]
。您可以使用两个主要选项

  • ApplicationContext
    注入到配置类中,将其强制转换为
    resourcepatternsolver
    并使用
    getResources
    方法。所有Spring默认应用程序上下文实现实现
    ResourcePatternResolver
    接口
  • 创建一个带有或不带有前面提到的上下文的
    PathMatchingResourcePatternResolver
  • 与注入的
    资源加载程序一起使用
  • 使用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;
        }
    }
    

    注入
    ResourceLoader
    ApplicationContext
    。用它构造一个
    PathMatchingResourcePatternResolver
    (您也可以尝试在没有上下文或资源加载器的情况下创建)并在模式中使用
    getResources
    方法来获取资源。我不会抛出异常,但只要添加
    throws IOException
    ,如果加载资源时出错,您可能不想启动应用程序。谢谢。好的观点:)修改的答案也反映了这一点。谢谢。使用PathMatchingPatternResolver解决了我的问题。自动布线在配置中不起作用(可能是因为它们尚未创建!)我将更新我的问题代码。
    Autowired
    在配置类中与普通组件一样起作用。但是,没有
    路径匹配模式解析器
    ,因此您必须构建自己(这也是我在代码片段中所展示的)。注入
    ResourceLoader
    ApplicationContext
    应该可以工作。注入
    ResourceLoader
    或'ApplicationContext'会导致空指针异常。当应用程序上下文初始化时,它们为null,应该可以工作,因为我自己已经多次使用过它。您的设置中一定有什么奇怪/错误。无法自动连线
    applicationContext
    首先是我的问题。我可以把我的配置放在哪里,这样你就可以看到,因为在这里添加它作为答案是不正确的。它正在毫无问题地工作。只是想知道问题出在哪里
    @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;
        }
    }