4

我正在嘗試配置spring配置中的推土機。當使用xml配置時,它會像在spring配置文件中設置資源

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

注入'ResourceLoader'或'ApplicationContext'。用它構建一個'PathMatchingResourcePatternResolver'(你也可以嘗試在沒有上下文或資源加載器的情況下創建),並使用'getResources'方法來獲取資源。 –

+0

我不會拋出一個異常,但只要加載'拋出IOException'如果出現問題加載資源,你可能不想啓動你的應用程序。 –

+0

謝謝。好點:) – alizelzele

回答

6

您需要使用ResourcePatternResolverclasspath*:dozer/**/*.dzr.xml轉化爲Resource[]。有兩個主要選項可以使用。

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

謝謝。使用PathMatchingPatternResolver解決了我的問題。自動裝配將無法在配置中工作(也許是因爲它們還沒有創建!)我將更新我的問題將代碼。 – alizelzele

+0

''Autowired''在配置類中就像正常組件一樣工作。但是沒有'PathMatchingPatternResolver',所以你必須構建自己(這也是我在代碼片段中展示的內容)。應注入'ResourceLoader'或'ApplicationContext'。 –

+0

注入'ResourceLoader'或'ApplicationContext'導致空指針異常。它們在應用程序上下文初始化時爲空 – alizelzele

相關問題