6、DI依赖注入
6.1、构造器注入
6.2、Set方式注入
- 依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
【环境搭建】
1.复杂类型
public class Address{
private String address;
public String getAddress(){
return address;
}
public void setAddress(string address) {
this.address = address;
}
}
2.真实测试对象
public class Student {
private string name;
private Address address;
private string[] books;
private List<string> hobbys;
private Map<string,string> card;
private set<string> games;
private string wife;
private Properties info;
}
3.bean.xml
<?xm1 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2081/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https: //www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.kuang.pojo.Address">
<property name="address" value="中国nb" / >
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!-- 第一种,普通值注入,value(string类型或者基本类型) -->
<property name="name" value="Spring" / >
<!--第二种,Bean注入,ref(引用类型)-->
<property name="address" ref="address" />
<!--第三种,数组注入 -->
<property name="books" />
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
< ! --第四种,List注入 -->
<property name="hobbys" />
<list>
<value>听歌</value>
<value>踢球</value>
<value>学习</value>
<value>敲代码</value>
</list>
</property>
<!--第五种,Map注入 -->
<property name="card" />
<map>
<!--entry是实体的意思 -->
<entry key=”身份证” value=”112233199800008888”/>
<entry key=”银行卡” value=”1234561231231234567”/>
</map>
</property>
<!--第六种,Set注入 -->
<property name="games" />
<set>
<value>LOL</value>
<value>DNF</value>
<value>CF</value>
</set>
</property>
<!--第七种,Null值注入 -->
<property name="wife" />
<null/>
</ property>
<!--第八种,Properties注入 -->
<property name="info" />
<props>
<prop key=”学号”>000001</prop>
<prop key=”ur;”>男</prop>
<prop key=”username”>root</prop>
<prop key=”password”>123456</prop>
</props>
</property>
</bean>
</bean>
4.测试类
public class MyTest {
public static void main(string[] args) {
Applicationcontext context = new classPathxmlApplicationcontext("beans.xml");
Student student = (student) context.getBean("student");
System. out.println(student.toString());
}
6.3、扩展方式注入
c命名p和命名空间注入:
使用:
<?xm1 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2081/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命名空间注入,可以直接注入属性的值:property -->
<bean id="user" class="com.kuang.pojo.User" p:name=”Spring” p:age=”18”/>
<!-- c命名空间注入,通过构造器(必须要有有参构造和无参构造)注入:construct-args-->
<bean id="user2" class="com.kuang.pojo.User" c:name=”Spring” c:age=”18”/>
</beans>
测试:
@Test
public void test2(){
Applicationcontext context = new classPathxmlApplicationcontext( "userbeans.xml");
User user = context.getBean ("user2",user.class);
System.out.println(user);
}
注意点:p命名和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http:// www.springframework.org/ schema/p"
xmlns:c="http://www.springframework .org/schema/ c"
6.4、Bean的作用域
1.单例模式(Spring默认机制)(单线程推荐使用)(定义: 确保一个类只有一个实例,并为整个系统提供一个全局访问点 (向整个系统提供这个实例))
<bean id="user2" class="com.kuang.pojo.user" c:age="18" c:name="Spring"scope="singleton" />
2.原型模式:每次从容器中get的时候,都会产生一个新对象!(特别浪费资源,多线程推荐使用)
<bean id="user2" class="com.kuang.pojo.user" c:age="18" c:name="Spring"scope="prototype" />
3.其余的request、session、application、这些个只能在web开发中使用到