Spring03: 依赖注入 与Bean作用域
依赖注入(Dependency Injection)DI。
指Bean对象的创建依赖于容器 . Bean对象的依赖资源。
指Bean对象所依赖的资源 , 由容器来设置和装配。
通过构造器注入:
通过类的Set方法注入:
测试pojo类 :
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);
}
}
以下8个Student中的属性 对应xml配置文件中8种类型的注入方式(第七个是空值null注入)
<bean id="address" class="com.kuang.pojo.Address">
<property name="address" value="重庆"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!--第一种:普通注入,value常量注入-->
<property name="name" value="阿飞"/>
<!--第二种:Bean注入,ref引用最上面的id="address"的Bean-->
<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>
</list>
</property>
<!--第五种:map注入-->
<property name="card">
<map>
<entry key="身份证" value="1649469494894"/>
<entry key="银行卡" value="1234125777896"/>
<entry key="校园卡" value="1236822025"/>
</map>
</property>
<!--第六种:set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--第七种:null-->
<property name="wife">
<null/>
</property>
<!--第八种:Properties-->
<property name="info">
<props>
<prop key="学号">8002181142</prop>
<prop key="班级">软工06班</prop>
<prop key="url">南京东路</prop>
</props>
</property>
</bean>
测试代码
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
运行结果
命名空间注入
User.java
public class User {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
1、p命名空间注入 : 需要在头文件中加入约束文件
不需要构造方法
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" p:name="狂神老师" p:age="22"/>
2、c 命名空间注入 : 需要在头文件中加入约束文件
在User类中添加有参构造方法,c为构造器注入,必须加上有参构造!
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" c:name="狂神老师" c:age="22"/>
测试代码
@Test
public void test02(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
分别运行的结果:
Bean的作用域
总共有四种,后两种是在基于web的Spring ApplicationContext情形下有效,这里讨论单例singleton和多例prototype
Singleton:
Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。
当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。系统默认是单例。
<bean id="user" class="com.kuang.pojo.User" scope="singleton">
以下测试返回true
@Test
public void test03(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
System.out.println(user==user2);
}
Prototype:
Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。
当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
<bean id="user" class="com.kuang.pojo.User" scope="singleton">
以下测试返回false
@Test
public void test03(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
System.out.println(user==user2);
}