简体   繁体   中英

Autowire a class that extends a collection in Spring

I've got a class that extends a collection, specifically a HashMap. I'd like to use it as an autowired field in another class, however when I try to use the @autowired or @resource annotations in the following way:

@Autowired
private myCollectionClass<String, Object> myCollectionClass;

I get the error: no qualifying bean of type [my collection class]

How can I autowire the class?

(I cannot use xml in the project)

The simplest solution is to use @Resource specifying the bean id

@Resource(name="myCollection")
private MyCollectionClass<String, Object> myCollectionClass;

Or you could use @Qualifier in conjunction with @Autowired

@Autowired @Qualifier("myCollection")
private MyCollectionClass<String, Object> myCollectionClass;

have you annotated myCollectionClass ?

If not, You need to annotate myCollectionClass as well with @Component for spring container to qualify the object based on class name or class type.

You can also use @Qualifier and specify the qualifier class name.

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