Spring中的內部Bean


簡介

當一個bean僅被用作另一個bean的屬性時,它能被聲明為一個內部bean,為了定義inner bean,在Spring 的 基於XML的 配置元數據中,可以在 <property/>或 <constructor-arg/> 元素內使用<bean/> 元素,內部bean通常是匿名的,它們的Scope一般是prototype

代碼示例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person2" class="com.itdjx.spring.dependency.injection.Person">
        <property name="name" value="李玉"/>
        <property name="age" value="23"/>
        <property name="sex" value="女"/>
        <property name="car" >
            <bean class="com.itdjx.spring.dependency.injection.Car">
                <constructor-arg value="Ferrari" index="0"/>
                <constructor-arg value="Italy" index="1"/>
                <constructor-arg value="22500000" type="double"/>
            </bean>
        </property>
    </bean>

</beans>

 


免責聲明!

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



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