How to load a bean defined without id, name in spring?

How to load a bean without id or name? Also how will spring differentiate if I define 2 beans of the same class without id or name

<bean class="xyz" />
<bean class="xyz" scope="prototype" />

it is written that

If there is another bean with the same name, a unique name will be created

How does spring deal with this?

+3
source share
1 answer

How to load a bean without id or name?

You can load beans by type:

applicationContext.getBeansOfType(xyz.class);

The above will return the map from the (generated) id to the bean instance.

how will spring differentiate if I define 2 beans of the same class without id or name

If you use autwiring by type, it throws an exception (two beans of the same type). You cannot autwire by name because there is no name.

+4
source

All Articles