您的位置:首页 > 编程语言 > Java开发

spring-依赖注入

2017-05-28 17:12 148 查看
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命名空间注入

9d5f
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring bean ioc