Reputation: 1543
I have a bean, into which I wanted to inject an array of Resources.
org.springframework.core.io.Resource[]
Actually, an array of InputStreamSource was good enough. I wanted my bean definition to look something like:
<bean id="..." class="com.usta.SomeClass">
<constructor-arg value="classpath:somedir/*.xml"/>
</bean>
where my constructor was:
public SomeClass(InputStreamSource[] sources);
Since that wouldn't work, I reluctantly chose to have
public SomeClass(InputStreamSource[] sources, ResourcePatternResolver resolver);
But now how can I inject the ApplicationContext (which is a ResourcePatternResolver) into this bean via constructor injection? Or can I say only auto-wire this constructor argument?
I know Setter Injection (with ResourceLoaderAware) would solve this but I am sticking to Consructor Injections as far as possible.
SomeClass uses the Resources some initialization up front; with setter injection I will have to defer initialization and not be able to declare a number of SomeClass's fields final.
Upvotes: 1
Views: 1147
Reputation: 2024
This should work for you ...
Constructor:
public SomeClass(final Resource[] resources) { ... }
Configuration:
<bean id="patternResolver" class="org.springframework.core.io.support.PathMatchingResourcePatternResolver" />
<bean id="..." class="com.usta.SomeClass">
<constructor-arg value="#{patternResolver.getResources('somedir/*.xml')}" />
</bean>
Upvotes: 1