What are inner ...
What are inner ...
Inner beans in Spring are beans that are defined within the scope of another bean, essentially serving as a property of the enclosing bean. They are typically used when a bean is only required for one particular property and nowhere else in the application. Inner beans are declared inside the <property>
or <constructor-arg>
tags of a bean definition in the Spring XML configuration file. They are always anonymous and scoped as prototypes, meaning they are created anew each time they are injected into the enclosing bean[1][3][4][5][7][8][9].
For example, if you have a TextEditor
bean that depends on a SpellChecker
bean, but the SpellChecker
is only used within the TextEditor
and not elsewhere, you can define the SpellChecker
as an inner bean of the TextEditor
bean like so:
<bean id="textEditor" class="com.example.TextEditor">
<property name="spellChecker">
<!-- This is the inner bean -->
<bean class="com.example.SpellChecker"/>
</property>
</bean>
In this configuration, the SpellChecker
bean is defined within the context of the TextEditor
bean, and it does not need to be declared with an id
or name
attribute since it will not be accessed directly from the Spring container but only through the TextEditor
...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào