Spring依赖注入(DI)
目录
构造器注入
set注入
拓展注入
bean的作用域
Singleton
Prototype
Dependency Injection
-
依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
-
注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .
构造器注入
具体实现:SpringIOC创建对象的方式_March€的博客-CSDN博客
set注入
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is....
测试domain类:
Address.java
public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
Student.java
package com.kuang.pojo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
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;public void setName(String name) {this.name = name;}public void setAddress(Address address) {this.address = address;}public void setBooks(String[] books) {this.books = books;}public void setHobbys(List<String> hobbys) {this.hobbys = hobbys;}public void setCard(Map<String, String> card) {this.card = card;}public void setGames(Set<String> games) {this.games = games;}public void setWife(String wife) {this.wife = wife;}public void setInfo(Properties info) {this.info = info;}public void show(){System.out.println("name="+ name+ ",address="+ address.getAddress()+ ",books=");for (String book:books){System.out.print("<<"+book+">>\t");}System.out.println("\n爱好:"+hobbys);System.out.println("card:"+card);System.out.println("games:"+games);System.out.println("wife:"+wife);System.out.println("info:"+info);}
}
1.常量注入
<bean id="student" class="com.ffyc.domain.Student"><property name="name" value="张三"></property>
</bean>
2.bean注入
这里的值是一个引用,ref
<bean id="address" class="com.ffyc.domain.Address"><property name="address" value="西安"/>
</bean>
<bean id="student" class="com.ffyc.domain.Student"><property name="name" value="张三"/><property name="address" ref="address"/>
</bean>
3.数组注入
<property name="books"><array><value>西游记</value><value>红楼梦</value><value>水浒传</value></array>
</property>
4.List注入
<property name="hobbys"><list><value>唱歌</value><value>跳舞</value></list>
</property>
5.map注入
<property name="card"><map><entry key="电话" value="123456"></entry><entry key="账户" value="1111"></entry></map>
</property>
6.set注入
<property name="games"><set><value>lol</value><value>王者荣耀</value></set></property>
7.Null注入
<property name="wife"><null></null>
</property>
8.properties注入
<property name="info"><props><prop key="学号">123</prop><prop key="性别">男</prop></props>
</property>
测试类:
拓展注入
1、P命名空间注入 : 需要在头文件中导入约束文件
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.ffyc.domain.User" p:name="张三"/>
2、c 命名空间注入 : 需要在头文件中导入约束文件
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.ffyc.domain.User" c:name="张三"/>
bean的作用域
在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象
Singleton
Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。
<bean id="user" class="com.ffyc.domain.User" scope="singleton">
Prototype
Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。