spring的List List 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 />
    <bean />

    <!-- mouse -->
    <bean >
        <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);
    }
}

结果:

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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2021-08-18
  • 2022-12-23
  • 2021-05-28
  • 2021-12-08
  • 2022-12-23
  • 2021-06-30
  • 2022-12-23
相关资源
相似解决方案
粤ICP备22038628号Powered By WordPress