什么是 spring 的內部 bean?


只有將 bean 用作另一個 bean 的屬性時,才能將 bean 聲明為內部 bean。

為了定義 bean,Spring 的基於 XML 的配置元數據在 <property>

<constructor-arg> 中提供了 <bean> 元素的使用。內部 bean 總是匿名

的,它們總是作為原型。

例如,假設我們有一個 Student 類,其中引用了 Person 類。這里我們將只創

建一個 Person 類實例並在 Student 中使用它。

Student.java

public class Student {

private Person person;

//Setters and Getters

}

public class Person {

private String name;

private String address;

//Setters and Getters

}

bean.xml

<bean id=“StudentBean" class="com.edureka.Student">

<property name="person">

<!--This is inner bean -->

<bean class="com.edureka.Person">

<property name="name" value=“Scott"></property>

<property name="address" value=

“Bangalore"></property>

</bean>

</property>

</bean>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM