I know that idref
is used to pass the id of a bean (i.e String value but not the reference). I was asked in an interview, what can be the use case of idref
? Why and when it should be used?
What can be the use case for Spring bean idref?
146 views Asked by SSC At
1
idref
just adds a layer of security by validating at deployment time that the referenced, named bean actually exists.A very nice example with appropriate explanation is provided in this link
Example
can also be written as
The advantage of using first way is already described.
If you use the second method then no validation is performed on the value that is passed to the
abcName
property of thexyz
bean i.e. during the loading of the context.Errors are only discovered when the
xyz
bean is instantiated. So, we can never know if any typos have been injected by the developer during the instantiation phase.Note : It is just a recommended approach, not a mandatory approach