SSM框架系列——配置文件
- 概述
- < bean >元素的常用属性
- Spring Bean作用域
- 创建时机
- singleton
- prototype
- 运用工厂来创建
- UserDaoFactory
- UserDaoImpl
- applicationContext.xml
- Application测试
概述
Spring 配置文件支持两种格式,即 XML 文件格式和 Properties 文件格式
- Properties 配置文件主要以 key-value 键值对的形式存在,只能赋值,不能进行其他操作,适用于简单的属性配置。
- XML 配置文件是树形结构,相对于 Properties 文件来说更加灵活。XML 配置文件结构清晰,但是内容比较繁琐,适用于大型复杂的项目。
通常情况下,Spring 的配置文件使用 XML 格式。XML 配置文件的根元素是 ,该元素包含了多个子元素 。每一个 元素都定义了一个 Bean,并描述了该 Bean 如何被装配到 Spring 容器中。
<?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.example.HelloWorld">
<property name="message" value="Hello World!" />
</bean>
</beans>
< bean >元素的常用属性
属性名称 | 描述 |
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属性指定构造参数的序号(从О开始),type属性指定构造参数的类型 |
property | < bean >元素的子元素,用于调用Bean 实例中的 setter方法来属性赋值,从而< property>和< constructor-arg>等元素的子元索,该元素中的bean属性用 |
ref | < property>和< constructor-arg>等元素的子元索,该元素中的bean属性用于指定对某个Bean 实例的引用 |
value | < property>和< constractor-arg> 等元素的子元素,用于直接指定一个常量值 |
list | 用于封装List 或数组类型的依赖注入 |
set | 用于封装Set类型的依赖注入 |
map | 用于封装 Map类型的依赖注入 |
entry | < map>元素的子元素,用于设置一个键值对。其key属性指定字符串类型的键值,ref 或 value子元素指定其值 |
init-method | 类初始化方法的名称 |
destroy-method | 类销毁方法的名称 |
Spring Bean作用域
在配置文件中,除了可以定义 Bean 的属性值和相互之间的依赖关系,还可以声明 Bean 的作用域。例如,如果每次获取 Bean 时,都需要一个 Bean 实例,那么应该将 Bean 的 scope 属性定义为 prototype,如果 Spring 需要每次都返回一个相同的 Bean 实例,则应将 Bean 的 scope 属性定义为 singleton。
作用域 | 范围 |
singleton | 默认,单例 |
prototype | 多例 |
request | WEB项目中,Spring创建一个 Bean的对象,将对象存入到request域中 |
session | WEB项目中,Spring创建一个 Bean的对象,将对象存入到session域中 |
global session | WEB项目中,应用在Portlet环境,如果没有Portlet环境那么globalSession相当于Session |
创建时机
singleton
当scope的取值为singleton时
Bean的实例化个数:1个
Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例
Bean的生命周期:
对象创建:当应用加载。创建容器时,对象就被创建了
对象运行:只要容器在,对象一直活着
对象销毁:当应用卸载,销毁容器时,对象就被销毁了
prototype
当scope的取值为prototype时
Bean的实例化个数:多个
Bean的实例化时机:当调用getBean0方法时实例化Bean
对象创建:当使用对象时,创建新的对象实例
对象运行:只要对象在使用中,就一直活着
对象销毁:当对象长时间不用时,被Java的垃圾回收器回收了
运用工厂来创建
解释:
使用工厂间接创建类,我们需要在bean里配置工厂
在类中获取工厂(factory-bean),以及工厂的方法(factory-method) 进行指定
UserDaoFactory
package com.example.factory;
import com.example.dao.UserDao;
import com.example.dao.impl.UserDaoImpl;
public class UserDaoFactory {
public UserDao userDaoFactory(){
return new UserDaoImpl();
}
}
UserDaoImpl
package com.example.dao.impl;
import com.example.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void show() {
System.out.println("UserDaoImpl!");
}
@Override
public void init() {
System.out.println("初始化");
}
public UserDaoImpl() {
System.out.println("构造");
}
}
applicationContext.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.xsd">
<bean id="userDaoFactory" class="com.example.factory.UserDaoFactory"></bean>
<bean id="userDao" class="com.example.dao.impl.UserDaoImpl" factory-bean="userDaoFactory"
factory-method="userDaoFactory" init-method="init"></bean>
</beans>
Application测试
package com.example;
import com.example.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Applictaion {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) app.getBean("userDao");
userDao.show();
}
}