简体   繁体   中英

How to use wildcards when searching for resources in Java-based Spring configuration?

I'm in the process of converting our XML- to a Java-based Spring 3 configuration, and couldn't find a way to "translate" this bean which uses wildcards for resource paths:

<bean id="messageSource" class="MyResourceBundleMessageSource">
    <property name="resources" value="classpath*:messages/*.properties" />
</bean>

The corresponding class looks like:

    public class MyResourceBundleMessageSource 
        extends org.springframework.context.support.ResourceBundleMessageSource {
      ...
      public void setResources(org.springframework.core.io.Resource... resources) 
         throws java.io.IOException { ... }
      ... 
    } 

Enumerating all the files "manually" is no option, as this is a multi-module project with quite a few files, and I would like to avoid changing the bean class as well (as it is actually located in a common library).

Following Sotirios Delimanolis' advice, I got it working:

@Bean
public MyResourceBundleMessageSource messageSource() throws IOException {
    MyResourceBundleMessageSource messageSource = new MyResourceBundleMessageSource();
    messageSource.setResources(new PathMatchingResourcePatternResolver().getResources("classpath*:messages/*.properties"));
    return messageSource;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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