二、SpringBean
由Spring IoC容器管理的对象称为Bean,Bean根据Spring配置文件中的信息创建。
Spring配置文件支持两种格式,即XML文件格式和Properties文件格式。
- Properties:key-value,赋值和取值,适用简单的属性配置。
- XML:树形结构,但是内容较为繁琐,适用于大型复杂项目。
Beans.xml:
<?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="helloWorld" class="com.jsonliu.bean.HelloWorld">
<property name="message" value="Hello World!" />
</bean>
</beans>
2.1、元素的常用属性
属性名称 | 描述 |
---|---|
id | Bean 的唯一标识符,Spring 容器对 Bean 的配置和管理都通过该属性完成。id 的值必须以字母开始,可以使用字母、数字、下划线等符号。 |
name | name 属性中可以为 Bean 指定多个名称,每个名称之间用逗号或分号隔开。Spring 容器可以通过 name 属性配置和管理容器中的 Bean。 |
class | 该属性指定了 Bean 的具体实现类,它必须是一个完整的类名,即类的全限定名。 |
scope | 用于设定 Bean 实例的作用域,属性值可以为 singleton(单例)、prototype(原型)、request、session 和 global Session。其默认值是 singleton |
constructor-arg | 元素的子元素,可以使用此元素传入构造参数进行实例化。该元素的 index 属性指定构造参数的序号(从 0 开始),type 属性指定构造参数的类型 |
property | 元素的子元素,用于调用 Bean 实例中的 setter 方法来属性赋值,从而完成依赖注入。该元素的 name 属性用于指定 Bean 实例中相应的属性名 |
ref | 和 等元素的子元索,该元素中的 bean 属性用于指定对某个 Bean 实例的引用 |
value | 和 等元素的子元素,用于直接指定一个常量值 |
list | 用于封装 List 或数组类型的依赖注入 |
set | 用于封装 Set 类型的依赖注入 |
map | 用于封装 Map 类型的依赖注入 |
entry | |
init-method | 容器加载 Bean 时调用该方法,类似于 Servlet 中的 init() 方法 |
destroy-method | 容器删除 Bean 时调用该方法,类似于 Servlet 中的 destroy() 方法。该方法只在 scope=singleton 时有效 |
lazy-init | 懒加载,值为 true,容器在首次请求时才会创建 Bean 实例;值为 false,容器在启动时创建 Bean 实例。该方法只在 scope=singleton 时有效 |
2.2、Spring Bean的作用域
作用域:如果每次获取Bean时,都需要一个Bean实例,那么应该将Bean的Scope属性定义为prototype,如果需要每次都返回一个相同的Bean实例,则应将Bean的scope属性定义为singleton。
作用域种类:
- singleton:默认值,单例模式,表示在Spring容器中只有一个Bean实例,Bean以单例方式存在。
- prototype:原型模式,表示每次通过Spring容器获取Bean时,容器都会创建一个Bean实例。
- request:每次HTTP请求时,容器都会创建一个Bean实例。该作用域只在当前HTTP Request内有效。
- session:同一个HTTP Session共享一个Bean实例,不同的Session使用不同的Bean实例。该作用域仅在当前HTTP Session内有效。
- application:同一个web应用共享一个Bean实例,该作用域在当前ServletContext内有效。类似于singleton,不同的是,singleton表示每个IoC容器中仅有一个Bean实例,而同一个Web应用找那个可能会有多个IoC容器,但一个Web应用中只会有一个SevletContext,application才是web应用中货真价实的单例模式。
- websocket:在整个websocket中有效。
2.3、singleton
Spring容器默认的作用域。当Bean的作用域为singleton时,Spring容器中只会存在一个共享的Bean实例。该Bean实例将存储在高速缓存中,并且对Bean的请求,只要id与该Bean定义匹配,都会返回该缓存对象。
通常情况,这种单例模式对于无会话状态的Bean(dao、service),是理想选择。
在spring配置文件中,配置方式:
<bean id="..." class="..." scope="singleton"/>
例1:
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("message : " + message);
}
}
public class MainApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.setMessage("对象A");
objA.getMessage();
HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
objB.getMessage();
}
}
<?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="helloWorld" class="com.jsonliu.bean.HelloWorld" scope="singleton">
</bean>
</beans>
运行结果
2.4、prototype
Spring容器会在每次请求该Bean时都创建一个新的Bean实例。prototype作用域适用于需要保持会话状态的Bean.
配置方式
<bean id="..." class="..." scope="prototype"/>
例2:
修改例1中Beans.xml
<?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="helloWorld" class="net.biancheng.HelloWorld" scope="prototype"/>
</beans>
运行结果