简体   繁体   中英

Spring @Resource not loading list of beans

When I try to load a resource, it never gets loaded and instead comes back as null . My code is as follows:

Controller.java

@Controller
@Path("/users")
public class UsersAPI {
    @Resource(name = "dataLoaderList")
    private List<DataLoader> dataLoaderList;
}

spring.xml

<context:component-scan base-package="com.my.package" />

<import resource="classpath:spring/commons.xml" />
<import resource="spring-dataloader.xml" />
<import resource="spring-security.xml" />

spring-dataloader.xml

<bean id="dataLoaderList"
    class="org.springframework.beans.factory.config.ListFactoryBean">
    <property name="sourceList">
        <list>
            <ref bean="dataLoader1" />
            <ref bean="dataLoader2" />
            <ref bean="dataLoader3" />
        </list>
    </property>
</bean>

What could I be missing?

EDIT:
I tried loading the resource in a JUnit Test, and it worked. So I have no idea why it wouldn't work in my Controller.

junit

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring.xml")
@ActiveProfiles("dev")
public class Test {

   @Resource(name = "dataLoaderList")
   private List<DataLoader> dataLoaderList;

}

I had to change @Controller annotation to @Component and that fixed my error. I have no idea why it worked.

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