DI依赖注入:
构造器注入:
前面说过了…
set方法注入(重点):
依赖注入:Set注入
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性由容器来注入
【环境搭建】:
复杂类型:
真实测试对象:
Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.xkl.pojo.Address">
<property name="address" value="长沙"/>
</bean>
<bean id="student" class="com.xkl.pojo.Student">
<!-- 基于name value的普通值注入-->
<property name="name" value="小恐龙"></property>
<!-- 第二种,Bean注入,ref-->
<property name="address" ref="address" />
<!-- 第三种,数组,array-->
<property name="books" >
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!-- 第四种,列表,list-->
<property name="hobbies">
<list>
<value>篮球</value>
<value>足球</value>
<value>游戏</value>
<value>编程</value>
</list>
</property>
<!-- 第五种,Map-->
<property name="card">
<map>
<entry key="身份证" value="12345612356123"></entry>
<entry key="银行卡" value="12123121141241"></entry>
</map>
</property>
<!-- 第六种,Set-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>和平精英</value>
<value>金铲铲</value>
<value>英雄联盟</value>
</set>
</property>
<!-- 第七种 null注入 -->
<property name="wife">
<null/>
</property>
<!-- 第八种 properties
key = value
key = value
-->
<property name="info">
<props>
<prop key="driver">123456</prop>
<prop key="url">男</prop>
<prop key="username">小恐龙</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
拓展方式注入:
我们可以使用p命名空间或者c命名空间进行注入
官方解释:
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值-->
<bean id="user" class="com.xkl.pojo.User" p:name="小恐龙" p:age="18" ></bean>
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.xkl.pojo.User" c:age="19" c:name="小恐龙"></bean>
</beans>
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值-->
<bean id="user" class="com.xkl.pojo.User" p:name="小恐龙" p:age="18" ></bean>
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.xkl.pojo.User" c:age="19" c:name="小恐龙"></bean>
</beans>
注意点:p命名和c命名空间不能直接导入,需要导入xml配置:
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
bean的作用域
代理模式:(Spring默认模式)
<bean id="user2" class="com.xkl.pojo.User" c:age="19" c:name="小恐龙" scope="singleton"></bean>
原型模式:每次从容器中get的时候,都会产生新的对象
<bean id="user2" class="com.xkl.pojo.User" c:age="19" c:name="小恐龙" scope="prototype"></bean>
其余的application,request,session,这些只能在web开发中使用到!
看狂神的30分钟讲解单例模式
bean的自动装配:
自动转配是spring满足bean依赖的一种方式。
Spring会在上下文中自动寻找,并自动给bean装配属性!
Spring中有三种自动装配的方式
- 在xml中显式的配置
- 在java中显式配置
- 隐式的自动装配bean【重点】
本文含有隐藏内容,请 开通VIP 后查看