Spring bean with no id or name

Some beans are not required to be accessed by other beans in the context file or programmatically. So as mentioned by JacobM, they don't require an id or name as they're not referenced.

Such an example would be a PropertyPlaceholderConfigurer, which reads a property file, then allows for runtime property replacement in the context definition.

The example definition would be

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="myapp.properties" />
</bean>

The JavaDoc provides further documentation on this object, but further on in the file you can reference properties from your file by just using the standard template replace placeholder ${...}.


One possibility is that you can define a bean in place, and so you don't need an id since you don't need to refer to it from anywhere else. Say I have a Foo object that takes a Bar property:

<bean id="foo" class="Foo">
     <property name="bar">
         <bean class="Bar">
     </property>
</bean>

The Bar bean doesn't need a name because it's only used to set that one property.

Tags:

Spring