How do I make sure resources are injected in my configuration before it starts returning beans?

I'm trying to define a bean which is a list of beans I've got configured using the @Component annotation. I'm using spring 3.1.

My @Configuration class looks like

@Configuration
public class ReferenceDataConfig {
    private ResourceA resourceA;

    @Resource
    private ResourceB resourceB;

    @Resource
    private ResourceC resourceC;

    @Resource
    private ResourceD resourceD;

    @Bean (name = "myResourceList")
    public List<MyResource> getResourceLst() {
        return Arrays.asList(
                resourceA
                , resourceB
                , resourceC
                , resourceD
        );
    }

    @Resource  (name = "resourceA")
    public void setResourceA(ResourceA resourceA) {
        this.resourceA = resourceA;
    }

What I'm finding is that the getResourceLst is getting called before any of the resources have been initialised. When I added the setResourceA method I can see that it does eventually get called, but by the time it's called it's too late. The myResourceList bean has already been created.

How should I deal with this problem?

Thanks, Peter

Answers


Depending on what exactly you're trying to do, the solution may be simpler than you think.

If List<MyResource> is supposed to contain all beans of type MyResource, then you don't have to declare a separate bean for the list. You can just inject the list as you would inject any other bean and Spring will gather all beans of type MyResource that it finds in the application context and inject them all as a List<MyResource> object.


Possible bug in Spring I think. I gather what I have should work, however the following workaround does the trick for me :

@Configuration
public class ReferenceDataConfig {
    @Bean (name = "myResourceList")
    public List<MyResource> getResourceLst(ResourceA resourceA, ResourceB resourceB, ResourceC resourceC, ResourceD resourceD) {
        return Arrays.asList(
                resourceA
                , resourceB
                , resourceC
                , resourceD
        );
    }

Need Your Help

How to actually build data warehouse from existing database?

database data-warehouse etl

Hope so, this question fits in the Stack Overflow standard.

Sending an NSURLRequest to phpMyAdmin results in the NSData being the php source code

php mysql xcode phpmyadmin

I have had quite the night trying to solve problems after "Upgrading" to OS X Yosemite 10.6.6. After re-installing MySQL / phpMyAdmin / Apache / php5 I am still having problems with code that worked