Spring中的Bean
可以把Spring容器看做一个大型的工厂,而Spring容器中的Bean就是该工厂的产品
Spring容器支持两种格式的配置文件,分别为Properties文件格式和XML文件格式。
bean的配置
一个<bean>元素中包含很多属性,具体如下。
id:是一个Bean的唯一 标识符, Spring容器对Bean的配置、管理都通过该属性来完成
name:Spring容器同样可以通过此属性对容器中的Bean进行配置和管理,name属性中可以为Bean指定多个名称,每个名称之间用逗号或分号隔开(由于Bean的id属性在Spring容器中是唯一的,如果想给Bean添加别名
或者想使用些不合法的XML字符,如“/”,就可以通过name属性进行设定)
class:该属性指定了Bean的具体实现类,它必须是一个完整的类名,使用类的全限定名。
scope:用来设定Bean实例的作用域,其属性值有singleton(单例)、prototype(原型)、request.session和global Session。其默认值是singleton
constructor arg: bean 元素的子元素,可以使用此元素传人构造参数进行实例化。该元素的index属性指定构造参数的序号(从0开始),type属性指定构造参数的类型,其参数值可以通过ref属性或者value 属性直接指定,也可以通过ref 或value 元素指定
数值可以通过ref属性或者value 属性直接指定,也可以通过ref 或value素指定
property:bean元素的子元素.用于调用Bean实例中的Setter方法完成属性赋值从而完成依赖注入。该元素的name属性指定Bean实例中的相应属性名,属性值可通过ref或value属性直接指定。
ref:property、constructor-arg等元素的子元素,该元素中的 bean属性用于指定对Bean工厂中某个Bean实例的引用。
value: property 、constructor-arg等元素的子元素,用来直接指定一个 常量值。
list::用于封装List或数组类型的依赖注人。
set:用于封装Set类型属性的依赖注人。
map:用于封装Map类型属性的依赖注人。
entry::map元素的子元素,用于设置一一个键值对。其key属性指定字符串类型的键值,ref或value 子元素指定其值。
在配置文件文件中,通常一个普通Bean只需要定义Id(或者name)和class两个属性即可,定义Bean的方式如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="bean" class="cn.itcast.bean"></bean> </beans>
Bean的实例化
在面向对象的程序中,要想使用某个对象,就需要先实例化这个对象。在Spring中,实例化Bean有三种方式,分别为构造器实例化、静态工厂实例化和实例化工厂方式实例化。
构造器实例化 构造器实例化是指Spring容器通过Bean对应的类中默认的构造函数来实例化Bean。
1.Bean.java
package cn.itcast.instance.controller public class Bean{ }
2.Bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="bean" class="cn.itcast.instance.countroller.Bean"></bean> </beans>
3.InstanceTest.java
public class InstanceTest{ @Test public void Test{ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
}}
静态工厂方式实例化
1.MyBeanFactory.java
package cn.itcast.instance.static_factory public class MyBeanFactory{ public static Bean createBean(){ return new Bean; } }
2.Bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="bean" class="cn.itcast.instance.static_factory.MyBeanFactory" factory-method = "createBean"></bean> </beans>
3.InstanceTest.java
public class InstanceTest{ @Test public void Test{ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); }}
实例工厂方式实例化
1.MyBeanFactory.java
package cn.itcast.instance.static_factory public class MyBeanFactory{ public Bean createBean(){ return new Bean; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="MyBeanFactory" class="cn.itcast.instance.factory.MyBeanFactory"/> <bean id = "bean" factory-bean = "myBeanFactory" factory-method = "createBean"></bean> </beans>
3.InstanceTest.java
public class InstanceTest{ @Test public void Test{ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); }}
Bean的作用域
1.singleton:单例模式,使用singleton定义的Bean在Spring容器中将只有一个实例,也就是说,无论有多少个Bean引用它,始终指向同一个对象。这也是spring容器默认的作用域。
2.prototype:原型模式.每次通过Spring容器获取的prototype定义的Bean时,容器都将创建一个新的Bean实例。
3.request::在一-次HTTP请求中,容器会返回该Bean的同一个实例。而对不同的HTTP请求则会产生一个新的Bean, 而且该Bean仅在当前HTTP Request 内有效。
4.session:session: 在一一次HTTP Session中,容器会返回该Bean的同一个实例。而对不同的HTTP请求则会产生一个新的Bean,而且该Bean仅在当前HTTP Session内有效。
5.globalSession:在-个全局的HTTP Session中,容器会返回该Bean 的同一个实例。仅在使用protlet context 时有效。
Bean的生命周期
spring容器可以管理singleton作用域下Bean的生命周期,在此作用域下,Spring能够精确的知道该Bean何时被创建,何时初始化完成,以及何时被销毁,而对于prototype作用
域的Bean,spring只负责创建,当容器创建了Bean实例后,Bean的实例就交给客户端代码来管理,spring容器不在跟踪其生命周期
(1) Spring 实例化Bean。
(2)利用依赖注人来配置Bean中的所有属性值。
(3)如果Bean实现了BeanNameAware接口,则Spring调用Bean的setBeanName()方法传入当前Bean的ID值。
(4)如果Bean实现了BeanFactoryAware接口,则Spring调用setBeanFactory()方法传入当前工厂实例的引用。.
(5)如果Bean实现了ApplicationContextAware接口,则Spring调用setApplicationContext()方法传入当前ApplicationContext实例的引用。
(6)如果Bean实现了BeanPostProcessor 接口,则Spring 会调用postProcessBeforeInitialzation()方法。
(7)如果Bean实现了InitializingBean 接口,则Spring会调用afterpropertiesSet()方法。
(8)如果在配置文件中通过init-method属性指定了初始化方法,则调用该初始化方法。
(9)如果Bean实现了BeanPostProcessor 接口,则Spring 会调用postProcessAfterInitialization()方法。
(10)此时, Bean实例化完成,就可以被使用了,它将一直存在于Spring容器中,直到被销毁。
(11)如果Bean实现了DisposableBean接口,则Spring会调用destory()方法。
(12)如果在配置文件中通过destory-method属性指定了Bean的销毁方法,则可调用该方法。