|
From: James C. <jim...@do...> - 2005-01-07 15:55:58
|
Is there a way to define a bean property that is "optional"? If it = exists, wire it up, otherwise let it remain null? This can be useful when = creating Spring-enabled components.=20 For example, I may have an email bean wired to a user management bean. <bean id=3D"usermgmt" class=3D"my.UserManagement"> <property name=3D"emailService"> <ref bean=3D"usermgmt.emailService" /> </property> </bean> If the user provided a bean with a name of "usermgmt.emailService" my = class would use this service to send emails. If the bean was not provided, I = would skip sending emails or maybe use a default implementation. Perhaps an optional attribute would help? <property name=3D"emailService" optional=3D"true"> I think this will become a bigger deal as people start componentizing = there spring modules. One way to solve this now is provide the bean name instead of a bean reference to the UserManagement bean. Then this bean is responsible for looking up the bean reference and handling it accordingly whether it = exists or not. This is more in the style of JNDI lookups and not very IoC, plus = it makes my beans more spring aware than I would like. > -----Original Message----- > Beans that are supposed to be available for references by name indeed = need > to be defined at the root level. Inner beans are always anonymous and > usually don't carry a name. Defining a name for them only adds value = if > they > are aware of their own name (BeanNameAware). |