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);
    }
}

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

猜你喜欢

转载自blog.csdn.net/Start1234567/article/details/112261533