spring的List<String> List<Object> Map属性xml注入_代码

bean:

@Data
public class Food {
    private String color;
}
import lombok.Data;
@Data
public class Mouse {
    private String mouseName;
    private List<String> alias;
    private List<Food> foods;
    private Map<String, Food> sortFoods;
}

 

xml:

    <!-- Food bean用于给mouse注入-->
    <bean id="food1" class="com.readspring.bean.Food" p:color="yellow"/>
    <bean id="food2" class="com.readspring.bean.Food" p:color="green"/>

    <!-- mouse -->
    <bean id="mouse" class="com.readspring.bean.Mouse">
        <property name="mouseName" value="Sam" />
        <!-- List<String> -->
        <property name="alias">
            <list>
                <value>sam1</value>
                <value>sam2</value>
            </list>
        </property>
        <!-- List<Object> -->
        <property name="foods">
            <list>
                <ref bean="food1" />
                <ref bean="food2" />
            </list>
        </property>
        <!-- Map<String,Object> -->
        <property name="sortFoods">
            <map>
                <entry key="1" value-ref="food1"/>
                <entry key="2" value-ref="food2"/>
            </map>
        </property>
    </bean>

测试类:

public class GetBeanTest {
    public static void main(String[] args) throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-INF/applicationcontext.xml");
        Mouse mouse = (Mouse)ac.getBean("mouse");
        System.out.println(mouse);
    }
}

结果:

 

posted @ 2020-11-18 11:19  圣金巫灵  阅读(439)  评论(0编辑  收藏  举报