Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring bean with no id or name

Tags:

spring

I'm reviewing some Spring code, and I see a few bean defs that do not have an id or a name. The person who did it is not around to ask. The application is working fine. I am not familiar what this necessarily means. Anybody know if this means anything in particular?

like image 868
bmw0128 Avatar asked Nov 26 '08 21:11

bmw0128


People also ask

Is bean id mandatory in Spring?

Sr.No. This attribute is mandatory and specifies the bean class to be used to create the bean. This attribute specifies the bean identifier uniquely. In XMLbased configuration metadata, you use the id and/or name attributes to specify the bean identifier(s).

Does @bean have an id attribute?

getBean("bean-identifier"); . Take @Bean, the java equivalent of <bean> tag, you wont find an id attribute. you can give your identifier value to @Bean only through name attribute.

What is an anonymous bean in Spring?

a bean injected inline as dependency in other bean. a bean that implements InitializingBean and DisposableBean, so his methods are called by IoC container.

Does the id of a bean have to be unique throughout the XML file?

Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the BeanFactory or ApplicationContext the bean is hosted in.


1 Answers

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 ${...}.

like image 83
Spencer Kormos Avatar answered Oct 03 '22 08:10

Spencer Kormos