Spring 5.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

Injecting Collections and Properties data to either of the container types is pretty straightforward. For the XML-based ApplicationContext, developers must be cautious with the use of generics. If the Collections of data is needed to be type-specific, injection is streamlined to use only one type throughout the process either setting <value> as the wrapper or string object type or listing all <ref> object references to one type. Mixing both metadata requires us to drop Generics on our model classes; otherwise a ClassCastException will be thrown, which will cause a container loading error. The same exception is also encountered when the model class requires the collection of employee objects in one of its setters but then accidentally a lone department bean has been <ref> mapped to it.

In general, XML-based ApplicationContext is not type-safe and type-sensitive as to what types of collection the model classes need. Unlike in JavaConfig, the @Bean objects are created programmatically; the needed Generic types are recognized by the container during bean creation and initialization.