spring-依赖注入

1.依赖注入
依赖:指bean对象的创建依赖于容器。bean对象的依赖资源(对象,常量,空,等等)


2.spring的注入—构造器注入
Spring:IOC—控制反转(3)


3.Spring 注入—setter注入
要求被注入的属性必须有set方法。set方法的方法名由set+属性首字母大写。如果属性是boolean类型,没有get方法是is。
(a)常量注入

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

    <bean id="student" class="com.et.vo.Student">
        <property name="name" value="elliott"></property>
    </bean>

</beans>

(b)bean注入

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

    <bean id="addr" class="com.et.vo.address"/>    
    <bean id="student" class="com.et.vo.Student">
        <property name="name" value="elliott"/>
        <property name="addr" ref="addr"/>
    </bean>

</beans>

(c)数组注入

<property name="books">
            <array>
                <value>Java面向对象编程</value>
                <value>Python基本教程</value>
                <value>C语言程序设计</value>
            </array>
        </property>

(d)Lits注入

<property name="hobbies">
            <list>
                <value>篮球</value>
                <value>足球</value>
                <value>羽毛球</value>
            </list>
        </property>

(e)Map注入

<property name="cards">
            <map>
                <entry key="中国银行" value="111111"/>
                <entry>
                    <key><value>建设银行</value></key>
                    <value>222222</value>
                </entry>
            </map>
        </property>

(f)Set注入

<property name="games">
            <set>
                <value>英雄联盟</value>
                <value>阴阳师</value>
            </set>
        </property>

(g)NULL注入

<property name="wife">
            <null/>
        </property>

(h)properties注入

<property name="info">
            <props>
                <prop key="学号">2017000001></prop>
                <prop key="sex"></prop>
                <prop key="name">Elliott</prop>
            </props>
        </property>

(i)p命名空间注入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值