Spring 中 bean 多种数据类型注入

创建实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private int id;
    private String name;
    private Address addresses;
    private Date birthday;
    private List<Hobby> hobbies;
    private Map<String,Hobby> hobbyMap;
    private Set<Address> addressSet;
    private Properties info;
}

配置bean.xml 文件

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


    <bean id="student" class="com.cl.pojo.Student">
        <property name="id" value="1"></property>
        <property name="name" value="小明"></property>
        
        <!-- 对象 类型数据注入 -->
        <property name="addresses" ref="address"></property>
        
        <!-- Date日期类型数据注入 -->
        <property name="birthday" value="2021/1/6"></property>
        
        <!-- list类型数据注入 -->
        <property name="hobbies">
            <list value-type="com.cl.pojo.Hobby">
                <ref bean="Hobby01"></ref>
                <ref bean="Hobby02"></ref>
            </list>
        </property>
        
        <!-- Map类型数据注入 -->
        <property name="hobbyMap">
            <map key-type="java.lang.String" value-type="com.cl.pojo.Hobby">
                <entry key="01" value-ref="Hobby01"></entry>
                <entry key="02" value-ref="Hobby02"></entry>
            </map>
        </property>
        
        <!-- Set类型数据注入 -->
        <property name="addressSet">
            <set value-type="com.cl.pojo.Address">
                <ref bean="address"></ref>
            </set>
        </property>

        <!--properties 配置文件注入-->
        <property name="info">
            <props>
                <prop key="userName">root</prop>
                <prop key="passWord">123456</prop>
            </props>
        </property>
    </bean>

    <bean id="address" class="com.cl.pojo.Address">
        <property name="id" value="1"></property>
        <property name="address" value="迎风路"></property>
    </bean>

    <bean id="Hobby01" class="com.cl.pojo.Hobby">
        <property name="id" value="1"></property>
        <property name="explain" value="打篮球"></property>
    </bean>

    <bean id="Hobby02" class="com.cl.pojo.Hobby">
        <property name="id" value="2"></property>
        <property name="explain" value="唱歌"></property>
    </bean>

    <bean id="gogo" class="com.cl.pojo.Student" name="s1,s2 s3;s4">
        <property name="id" value="1"/>
        <property name="name" value="小号"/>
        <property name="addresses" ref="address"/>
    </bean>

</beans>

ApplicationContext获取 bean

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(student);
    }
}

测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值