Spring Bean知识点(一)

<?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"
    default-init-method="init"
    default-destroy-method="destroy">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" scope="singleton" init-method="inits" destroy-method="destroy">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>
  • xmlns="http://www.springframework.org/schema/beans":默认命名空间:它没有空间名,用于Spring Bean的定义;
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance":xsi命名空间:这个命名空间用于为每个文档中命名空间指定相应的Schema样式文件,是标准组织定义的标准命名空间。
  • default-init-method和default-destroy-method:默认的初始化和销毁方法。如果你有太多具有相同名称的初始化或者销毁方法的Bean,那么你不用在每一个Bean上声明初始化方法和销毁方法,使用default-init-method和default-destroy-method即可为所有的Bean提供默认的初始化和销毁方法。
  • class:该属性为强制性的,并且用来指定创建bean的bean类
  • name:指定唯一的bean标识符。在基于XML的配置元数据中,可以使用ID和/或name属性来指定bean标识符 
  • scope:指定bean定义创建的对象的作用域,有5种作用域:
                1)singleton(默认值):在Spring IoC容器中仅存一个Bean实例,Bean以单例方式存在
                    注:singleton是单例类型,就是在创建容器时就同时自动创建了一个bean的对象,不管你有没有使用,它都存在了,每次获取到的对象都是同一个对象。
                2)prototype:每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()
                    注:prototype是原型类型,它在我们创建容器时并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象
                        根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域
                注:接下来三个作用域仅适用于WebApplicationContext环境
                3)request:每次Http请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
                4)session:同一个Http Session共享一个Bean,不同的Session使用不同的Bean,该作用域仅适用于WebApplicationContext环境
                5)global-session:一般用于Portlet应用环境,该作用域仅适用于WebApplicationContext环境
  • constructor-arg、properties和autowiring mode:用来注入依赖关系
  • lazy-initialization mode(lazy-init):延迟初始化的bean告诉IoC容器在它第一次被请求时,而不是在启动时去创建一个bean实例
  • initalization方法(init-method):在bean的所有必需的属性被容器设置之后,调用回调方法(实例化bean时调用该方法)
  • destruction方法(destroy-method):当包含该bean的容器销毁时,使用回调方法(从容器中移除bean之后调用该方法)
  • 当在Beans头文件里面声明了默认的初始化和销毁方法(default-init-method和default-destroy-method),Bean里面也声明了初始化和销毁方法(init-method和destroy-method)时,会调用Bean的初始化和销毁方法,而不会调用默认方法。
  • 初始化回调

    org.springframework.beans.factory.InitializingBean 接口指定一个单一的方法:

    void afterPropertiesSet() throws Exception;

    因此,你可以简单地实现上述接口和初始化工作可以在 afterPropertiesSet() 方法中执行,如下所示:

    public class ExampleBean implements InitializingBean {
       public void afterPropertiesSet() {
          // do some initialization work
       }
    }

    在基于 XML 的配置元数据的情况(不继承接口),你可以使用 init-method 属性来指定带有 void 无参数方法的名称(在Bean中写入类中定义的方法名,void类型,不带参数)。例如:

    <bean id="exampleBean" 
             class="examples.ExampleBean" init-method="init"/>

    下面是类的定义:

    public class ExampleBean {
       public void init() {
          // do some initialization work
       }
    }

    销毁回调

    org.springframework.beans.factory.DisposableBean 接口指定一个单一的方法:

    void destroy() throws Exception;

    因此,你可以简单地实现上述接口并且结束工作可以在 destroy() 方法中执行,如下所示:

    public class ExampleBean implements DisposableBean {
       public void destroy() {
          // do some destruction work
       }
    }

    在基于 XML 的配置元数据的情况下,你可以使用 destroy-method 属性来指定带有 void 无参数方法的名称(同init)。例如:

    <bean id="exampleBean"
             class="examples.ExampleBean" destroy-method="destroy"/>

在同时继承接口并另外写了方法在Bean中声明时,会同时调用实现接口的方法和自己声明的方法。推荐自己声明初始化和销毁方法,尽量避免继承接口 。

猜你喜欢

转载自blog.csdn.net/burmem/article/details/87745112