Spring Set注入方式

Spring Set注入方式

<?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-4.0.xsd">

<!--    id ="变量名
        class="对象"
        -->


<!--    <bean id="hello" class="com.yang.bean.Hello" >-->
<!--    <property name="name" value="杨海彭" />-->
<!--   </bean>-->

<!--下标赋值-->
<!--    <bean id="hello" class="com.yang.bean.Hello">-->
<!--        <constructor-arg index="0" value="杨海彭"/>-->
<!--    </bean>-->
<!--通过类型创建,不推荐使用   基本类型直接写,应用类型全类名-->
<!--    <bean id="hello" class="com.yang.bean.Hello">-->
<!--        <constructor-arg type="java.lang.String" value="杨海彭"/>-->
<!--    </bean>-->
<!--第三种 直接通过参数名来设置-->
    <bean id="hello" class="com.yang.bean.Hello">
        <constructor-arg name="name" value="杨海彭"/>
    </bean>

    <bean id="address" class="com.yang.bean.Address">
        <property name="name" value="长沙"/>
    </bean>

    <bean id="user" class="com.yang.bean.User">

    </bean>

    <bean id="student" class="com.yang.bean.Student">
<!--        普通值注入   -->
        <property name="name" value="杨海彭"/>
<!--        bean 注入  -->
        <property name="address" ref="address"/>
<!--        数组  -->
        <property name="bppks">
            <array>
                <value>java开发</value>
                <value>MySQL数据库</value>
                <value>Linux操作系统</value>
            </array>
        </property>
<!--        LIst  -->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>篮球</value>
                <value>说错</value>
                <value>吉他</value>
                <value>画画</value>
            </list>
        </property>
<!--        Map   -->
        <property name="card">
            <map>
                <entry key="身份证" value="31231231232132132134"/>
                <entry key="银行卡" value="89789778687676899098"/>
            </map>
        </property>

<!--        Set-->
        <property name="games">
            <set>
                <value>云顶之亦</value>
                <value>CF</value>
                <value>DNF</value>
                <value>LOL</value>
            </set>
        </property>
<!--        null   -->
        <property name="wife" >
            <null/>
        </property>
<!--        Properties  -->
        <property name="info" >
            <props>
                <prop key="学号">68686787</prop>
                <prop key="年级">17</prop>
                <prop key="班级">401</prop>
            </props>
        </property>
     </bean>

</beans>

测试类

@Test
    public  void test1() {
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象现在都在Spring中的管理 ,我们要使用直接去取出来就可以了
        Student  student=context.getBean("student", Student.class);
        System.out.println(student);
    }

总结:即使再小的帆也能远航!!!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值