Spring中的Bean

能够把Spring容器看作一个大型的工厂,而Spring容器中的Bean就是该工厂的产品java

Spring容器支持两种格式的配置文件,分别为Properties文件格式和XML文件格式。spring

bean的配置数组

一个<bean>元素中包含不少属性,具体以下。session

  id:是一个Bean的惟一 标识符, Spring容器对Bean的配置、管理都经过该属性来完成app

  name:Spring容器一样能够经过此属性对容器中的Bean进行配置和管理,name属性中能够为Bean指定多个名称,每一个名称之间用逗号或分号隔开(因为Bean的id属性在Spring容器中是惟一的,若是想给Bean添加别名函数

或者想使用些不合法的XML字符,如“/”,就能够经过name属性进行设定)post

 class:该属性指定了Bean的具体实现类,它必须是一个完整的类名,使用类的全限定名。spa

scope:用来设定Bean实例的做用域,其属性值有singleton(单例)、prototype(原型)、request.session和global Session。其默认值是singletonprototype

constructor arg: bean 元素的子元素,可使用此元素传人构造参数进行实例化。该元素的index属性指定构造参数的序号(从0开始),type属性指定构造参数的类型,其参数值能够经过ref属性或者value 属性直接指定,也能够经过ref 或value 元素指定3d

数值能够经过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.sessionsession: 在一一次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的销毁方法,则可调用该方法。

相关文章
相关标签/搜索