SSM[spring] 基于XML的IOC使用

Bean的注意事项

创建bean的三种方式

  • 使用默认构造函数
    1
    <bean id="accountService" class="com.gphper.service.imp.AccountServiceImpl"></bean>
  • 使用其他类提供的方法获取对象
    1
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
  • 使用其他类中的静态方法创建
    1
    <bean id="accountService" class="instanceFactory" factory-method="getAccountService"></bean>

bean的作用范围 使用标签 scope 属性表示

  • singleton: 单例模式(默认)立即加载
  • prototype: 多例 延迟加载
  • request: 作用于web应用的请求范围
  • session: 作用于web应用的会话范围
  • global-session: 作用域集群环境的会话范围(全局会话范围),不是集群时,它就是session

bean 生命周期

  • 单例对象:
    出生:当容器创建时对象出生
    活着:只要容器还在,对象一直活着
    死亡:容器销毁,对象消亡
    总结:单例对象的生命周期和容器相同
  • 多例对象:
    出生:当我们使用对象时,spring创建
    活着:对象只要是在使用过程中就一直活着
    死亡:当对象长时间不用时,且不存在其他对象引用,则java的垃圾回收器回收

可以使用 init-methoddestroy-method设置创建和销毁的回调

1
<bean id="accountService" init-method="init" destory-method="destory" class="com.gphper.service.imp.AccountServiceImpl"></bean>

依赖注入

构造函数注入

<bean>内部使用标签 <constructor-arg>

标签属性:

查找参数方式:

  • index: 指定索引复制
  • type: 指定类型复制
  • name: 指定参数名称赋值

为参数赋值方式:

  • value: 对基础类型参数和String类型赋值
  • ref: 为其他bean类型赋值
[com.gphper.service.imp.AccountServiceImpl]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.gphper.service.imp;

import com.gphper.service.IAccountService;

import java.util.Date;

public class AccountServiceImpl implements IAccountService {

private String name;
private Integer age;
private Date birthday;

public AccountServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}

@Override
public void saveAccount() {

}

@Override
public String toString() {
return "AccountServiceImpl{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
1
2
3
4
5
6
<bean id="accountService" class="com.gphper.service.imp.AccountServiceImpl">
<constructor-arg name="age" value="10"></constructor-arg>
<constructor-arg name="name" value="gphper"></constructor-arg>
<constructor-arg name="birthday" ref="age"></constructor-arg>
</bean>
<bean id="age" class="java.util.Date"></bean>
[client.java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.gphper.ui;

import com.gphper.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
/**
* ApplicationContext 立即加载,加载完配置文件就创建了对象
* BeanFactory 延迟加载
* @param args
*/
public static void main(String[] args) {

ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);

System.out.println(as);
}
}

set方法注入

<bean>内部使用标签 <property>
这个标签中只有 namevalue ref 属性

[com.gphper.service.imp.AccountServiceImpl2]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.gphper.service.imp;

import com.gphper.service.IAccountService;

import java.util.Date;

public class AccountServiceImpl2 implements IAccountService {

private String name;
private Integer age;
private Date birthday;

public void setName(String name) {
this.name = name;
}

public void setAge(Integer age) {
this.age = age;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

@Override
public void saveAccount() {

}

@Override
public String toString() {
return "AccountServiceImpl{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
1
2
3
4
<bean id="accountService2" class="com.gphper.service.imp.AccountServiceImpl2">
<property name="age" value="20"></property>
<property name="name" value="gphper"></property>
</bean>

复杂类型注入 Array/List/Map/Set 等

  • 给list结构的集合注入标签有 <list> <array> <set>
  • 给Map结构集合的注入标签 <map> <props>
  • 对于结构相同的数据,标签之间可互换
    [com.gphper.service.imp.AccountServiceImpl3]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    package com.gphper.service.imp;

    import com.gphper.service.IAccountService;

    import java.util.*;

    public class AccountServiceImpl3 implements IAccountService {

    private Map<String,String> myMap;
    private List<Integer> myList;
    private Set<String> mySet;
    private Properties myProp;

    public void setMyMap(Map<String, String> myMap) {
    this.myMap = myMap;
    }

    public void setMyList(List<Integer> myList) {
    this.myList = myList;
    }

    public void setMySet(Set<String> mySet) {
    this.mySet = mySet;
    }

    public void setMyProp(Properties myProp) {
    this.myProp = myProp;
    }

    @Override
    public void saveAccount() {

    }


    @Override
    public String toString() {
    return "AccountServiceImpl3{" +
    "myMap=" + myMap +
    ", myList=" + myList +
    ", mySet=" + mySet +
    ", myProp=" + myProp +
    '}';
    }
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <bean id="accountService3" class="com.gphper.service.imp.AccountServiceImpl3">
    <property name="myList">
    <list>
    <value>1</value>
    <value>2</value>
    </list>
    </property>
    <property name="myMap">
    <map>
    <entry key="username" value="gphper"></entry>
    </map>
    </property>
    </bean>