Spring框架(3) —— Bean对象的配置方式

简介

  • Spring Bean对象
    • Bean对象 是构成应用程序的支柱。
    • Bean对象 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。
    • Bean对象 是由用 Spring IoC 容器提供的配置元数据创建的。
      • 把配置元数据提供给 Spring IoC 容器的方法:
        1. 基于 XML 的配置文件
        2. 基于注解的配置
        3. 基于 Java 的配置

属性

构成每个 Bean对象 定义的一组属性:

属性 描述
class 强制属性,用来指定创建 bean 的类。
name / id 唯一的 bean 标识符。
scope 指定 bean 对象的作用域
constructor-arg 注入依赖关系(无参构造)
properties 注入依赖关系
destroy-method 初始化方法
init-method 销毁方法

目录结构

  • src
    • main
      • java.cn.water
        • POJO.java(实体类)
        • POJOFactory.java(工厂类)
      • resources
        • LifeCycle
          • Beans.xml(Spring配置文件)
        • NewInstance
          • Beans.xml(Spring配置文件)
        • Scope
          • Beans.xml(Spring配置文件)
    • test
      • java.cn.water.test
        • LifeCycle
          • SpringTest.java(测试类)
        • NewInstance
          • SpringTest.java(测试类)
        • Scope
          • SpringTest.java(测试类)
  • pom.xml(Maven配置文件)

Maven配置文件

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.water</groupId>
<artifactId>section02_Bean</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- Spring框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>

实体类

POJO.java

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
package cn.water;

/**
* @author Water
* @date 2019/10/22 - 17:26
* @description 实体类
*/
public class POJO {

/* 成员变量 */
private String message;


/* 成员方法:初始化 */
public void init(){
System.out.println("POJO类:初始化成功!");
}

/* 成员方法:销毁 */
public void destroy(){
System.out.println("POJO类:销毁成功!");
}

/* 构造方法 */

public POJO() {
System.out.println("POJO类:实例化成功!");
}
}

工厂类

POJOFactory.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package cn.water;

/**
* @author Water
* @date 2019/10/23 - 10:36
* @description 工厂类
*/
public class POJOFactory {

/* 成员方法 */
public POJO getPOJO() {
return new POJO();
}

/* 静态方法 */
public static POJO havePOJO(){
return new POJO();
}

}

配置文件

LifeCycle

1
2
3
4
5
6
7
8
9
10
11
<?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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<bean id="pojo" class="cn.water.POJO" init-method="init" destroy-method="destroy"></bean>

</beans>

NewInstance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<!-- 无参构造 -->
<bean id="pojo01" class="cn.water.POJO"></bean>

<!-- 工厂类 成员方法 -->
<bean id="factory" class="cn.water.POJOFactory"></bean>
<bean id="pojo02" class="cn.water.POJO" factory-bean="factory" factory-method="getPOJO"></bean>

<!-- 工厂类 静态方法 -->
<bean id="pojo03" class="cn.water.POJOFactory" factory-method="havePOJO"></bean>


</beans>

Scope

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- 单例 -->
<bean id="pojo01" class="cn.water.POJO" scope="singleton"></bean>

<!-- 多例 -->
<bean id="pojo02" class="cn.water.POJO" scope="prototype"></bean>

</beans>

测试类

LifeCycle

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
package cn.water.test.LifeCycle;

import cn.water.POJO;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.beans.Beans;

/**
* @author Water
* @date 2019/10/23 - 10:10
* @description 测试Spring框架 Bean对象的生命周期
*/
public class SpringTest {


@Test
public void test01(){
/* 1、加载配置文件,初始化Bean对象 */
AbstractApplicationContext abstractApp = new ClassPathXmlApplicationContext("LifeCycle/Beans.xml");
/* -- init-method 方法执行 --- */
/* 2、获取Bean对象 */
POJO pojo = abstractApp.getBean("pojo", POJO.class);
/* 3、注销容器 */
abstractApp.registerShutdownHook();
/* -- destroy-method 方法执行 --- */
}

}

NewInstance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cn.water.test.NewInstance;

import cn.water.POJO;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author Water
* @date 2019/10/23 - 10:10
* @description 测试Spring框架 Bean对象的实例化
*/
public class SpringTest {


@Test
public void test01(){
/* 1、加载配置文件,初始化Bean对象 */
ApplicationContext app = new ClassPathXmlApplicationContext("NewInstance/Beans.xml");
}

}

Scope

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
47
48
49
package cn.water.test.Scope;

import cn.water.POJO;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
* @author Water
* @date 2019/10/23 - 8:39
* @description 测试Spring框架 Bean对象的作用域
*/
public class SpringTest {

/** 单例模式(默认) */
@Test
public void test01(){
/* 1、加载配置文件,初始化Bean对象 */
ApplicationContext app = new ClassPathXmlApplicationContext("Scope/Beans.xml");
/* 2、获取Bean对象 */
POJO pojo1 = app.getBean("pojo01", POJO.class);
POJO pojo2 = app.getBean("pojo01", POJO.class);
POJO pojo3 = app.getBean("pojo01", POJO.class);
/* 3、输出 */
System.out.println(pojo1);
System.out.println(pojo2);
System.out.println(pojo3);
}

/* 多例模式 */
@Test
public void test02(){
/* 1、加载配置文件,初始化Bean对象 */
ApplicationContext app = new ClassPathXmlApplicationContext("Scope/Beans.xml");
/* 2、获取Bean对象 */
POJO pojo1 = app.getBean("pojo02", POJO.class);
POJO pojo2 = app.getBean("pojo02", POJO.class);
POJO pojo3 = app.getBean("pojo02", POJO.class);
/* 3、输出 */
System.out.println(pojo1);
System.out.println(pojo2);
System.out.println(pojo3);
}


}

作用域

  • 当在 Spring 中定义一个 bean 时,你可以指定该 bean 的作用域。
    • 例如,为了强制 Spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean 的作用域的属性为 prototype
    • 同理,如果你想让 Spring 在每次需要时都返回同一个bean实例,你应该声明 bean 的作用域的属性为 singleton
作用域 描述
singleton 仅存在一个Bean实例,Bean以单例方式存在
(默认值)
prototype 每次从容器中调用Bean时,都返回一个新的实例,
(即每次调用getBean()时,相当于执行newXxxBean())
request 每次HTTP请求都会创建一个新的Bean
(仅适用于WebApplicationContext环境)
singleton 同一个HTTP Session共享一个Bean
不同Session使用不同的Bean
(仅适用于WebApplicationContext环境)
global-session 一般用于Portlet应用环境
(仅适用于WebApplicationContext环境)

目录结构

  • src
    • main
      • java.cn.water
        • POJO.java(实体类)
      • resources
        • Beans.xml(Spring配置文件)
    • test
      • java.cn.water.test
        • SpringTest.java(测试类)

实体类

1
2
3
4
5
6
7
8
package cn.water;

public class POJO {

/* 成员变量 */
private String message;

}

配置文件

  • 单例
    • bean标签
      • Scope属性:singleton
1
2
<!-- 单例 -->
<bean id="pojo01" class="cn.water.POJO" scope="singleton"></bean>
  • 多例
    • bean标签
      • Scope属性:prototype
1
2
<!-- 多例 -->
<bean id="pojo02" class="cn.water.POJO" scope="prototype"></bean>

测试类

  • 单例
1
2
3
4
5
6
7
8
9
10
11
12
13
@Test
public void test01(){
/* 1、加载配置文件,初始化Bean对象 */
ApplicationContext app = new ClassPathXmlApplicationContext("LifeCycle/Beans.xml");
/* 2、获取Bean对象 */
POJO pojo1 = app.getBean("pojo01", POJO.class);
POJO pojo2 = app.getBean("pojo01", POJO.class);
POJO pojo3 = app.getBean("pojo01", POJO.class);
/* 3、输出 */
System.out.println(pojo1);
System.out.println(pojo2);
System.out.println(pojo3);
}
  • 多例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Test
    public void test02(){
    /* 1、加载配置文件,初始化Bean对象 */
    ApplicationContext app = new ClassPathXmlApplicationContext("LifeCycle/Beans.xml");
    /* 2、获取Bean对象 */
    POJO pojo1 = app.getBean("pojo02", POJO.class);
    POJO pojo2 = app.getBean("pojo02", POJO.class);
    POJO pojo3 = app.getBean("pojo02", POJO.class);
    /* 3、输出 */
    System.out.println(pojo1);
    System.out.println(pojo2);
    System.out.println(pojo3);
    }

运行结果

  • 单例
1
2
3
cn.water.POJO@6321e813
cn.water.POJO@6321e813
cn.water.POJO@6321e813
  • 多例
1
2
3
cn.water.POJO@6321e813
cn.water.POJO@79be0360
cn.water.POJO@22a67b4

生命周期

  • 理解 Spring bean 的生命周期很容易。当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态。同样,当 bean 不再需要,并且从容器中移除时,可能需要做一些清除工作。
  • 为了定义安装和拆卸一个 bean,我们只要声明带有 init-method 和/或 destroy-method 参数的 。init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法。同样,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。
  • Bean的生命周期可以表达为:
    • Bean的定义——Bean的初始化——Bean的使用——Bean的销毁

目录结构

  • src
    • main
      • java.cn.water
        • POJO.java(实体类)
      • resources
        • Beans.xml(Spring配置文件)
    • test
      • java.cn.water.test
        • SpringTest.java(测试类)

实体类

  • 在实体类中,添加两个成员方法
    • 初始化方法
    • 销毁方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.water;

public class POJO {

/* 成员变量 */
private String message;

/* 成员方法:初始化 */
public void init(){
System.out.println("POJO类:初始化成功!");
}

/* 成员方法:销毁 */
public void destroy(){
System.out.println("POJO类:销毁成功!");
}

}

配置文件

  • 初始化方法
    • bean标签
      • init-method属性:方法名
  • 销毁方法
    • bean标签
      • destroy-method属性:方法名
1
<bean id="pojo" class="cn.water.POJO" init-method="init" destroy-method="destroy"></bean>

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test
public void test01(){

/* 1、加载配置文件,初始化Bean对象 */
AbstractApplicationContext abstractApp = new ClassPathXmlApplicationContext("LifeCycle/Beans.xml");

/* -- init-method 方法执行 --- */

/* 2、获取Bean对象 */
POJO pojo = abstractApp.getBean("pojo", POJO.class);

/* 3、注销容器 */
abstractApp.registerShutdownHook();

/* -- destroy-method 方法执行 --- */

}

运行结果

1
2
3
4
POJO类:初始化成功!


POJO类:销毁成功!

实例化

默认无参构造

实体类

1
2
3
4
5
6
7
8
9
10
11
12
package cn.water;

public class POJO {

/* 成员变量 */
private String message;

/* 构造方法 */
public POJO() {
System.out.println("POJO类:实例化成功!");
}
}

配置文件

  • 默认根据 无参构造函数 来创建Bean对象。
    • 实体类 全类名
1
<bean id="constructor_based" class="cn.water.POJO"></bean>

工厂类 成员方法

实体类

1
2
3
4
5
6
7
8
9
10
11
12
package cn.water;

public class POJO {

/* 成员变量 */
private String message;

/* 构造方法 */
public POJO() {
System.out.println("POJO类:实例化成功!");
}
}

工厂类

1
2
3
4
5
6
7
8
9
10
package cn.water;

public class POJOFactory {

/* 成员方法 */
public POJO getPOJO() {
return new POJO();
}

}

配置文件

  • 首先,根据 无参构造函数 来创建工厂类对象。
    • 工厂类 全类名
  • 然后,根据 工厂类的成员方法 来创建实体类对象。
    • 实体类 全类名
    • 工厂类 id
    • 工厂类 成员方法名
1
2
<bean id="factory" class="cn.water.POJOFactory"></bean>
<bean id="factory_Method_based" class="cn.water.POJO" factory-bean="factory" factory-method="getPOJO"></bean>

工厂类 静态方法

实体类

1
2
3
4
5
6
7
8
9
10
11
12
package cn.water;

public class POJO {

/* 成员变量 */
private String message;

/* 构造方法 */
public POJO() {
System.out.println("POJO类:实例化成功!");
}
}

工厂类

1
2
3
4
5
6
7
8
9
10
package cn.water;

public class POJOFactory {

/* 静态方法 */
public static POJO havePOJO(){
return new POJO();
}

}

配置文件

  • 直接根据 工厂类静态方法 来创建Bean对象。
    • 工厂类 全类名
    • 工厂类 静态方法名
1
<bean id="pojo03" class="cn.water.POJOFactory" factory-method="havePOJO"></bean>
-------------本文结束-------------
Donate comment here