Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 485
Landei Avatar asked Feb 15 '23 01:02

Landei


1 Answers

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;
}
like image 69
Landei Avatar answered Feb 17 '23 20:02

Landei