Spring容器

来源:互联网 发布:淘宝现金流量表 编辑:程序博客网 时间:2024/06/05 21:58

spring容器是什么?

spring框架中的一个核心模块,用于管理对象。

启动spring容器?

step1.导包(spring-webmvc)。

step2.添加配置文件(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"    xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"></beans>

step3.启动spring容器。

//启动Spring容器ApplicationContext ac =         new ClassPathXmlApplicationContext(                "applicationContext.xml");

如何创建对象?

方式一 使用无参构造器(重点)。

step1.给类添加无参构造器(或者缺省构造器)
step2.配置元素。

<!--     使用无参构造器创建对象    id属性:bean的名称,要求唯一。<br/>    class属性:类的全限定名(即要求包含包名) --><bean id="stu1" class="first.Student"></bean><bean id="date1" class="java.util.Date"></bean>

step3.调用容器的getbean方法来获得对象。

//获得对象Student stu = ac.getBean("stu1",Student.class);System.out.println(stu);Date date1 = ac.getBean("date1",Date.class);System.out.println(date1);

方式二 使用静态工厂方法(了解)。

通过调用类的静态方法来创建对象。

<!--      使用静态工厂方法创建对象    factory-method属性:指定一个静态方法。    spring容器会调用这个静态方法来创建对象。    注:Calendar没有构造器。--><bean id="cal1" class="java.util.Calendar" factory-method="getInstance"></bean>

方式三 使用实例工厂方法(了解)。

通过调用对象的实例方法来创建对象。

<!--     使用实例工厂方法创建对象。    factory-bean属性:指定一个bean的id。    factory-method属性:指定一个方法。    spring容器会调用这个bean的对应的方法来创建对象。 --><bean id="time1" factory-bean="cal1" factory-method="getTime"></bean>

作用域

默认情况下,容器对于某个bean,只会创建一个实例。

可以设置scope属性值为prototype,这样,容器对于某个bean会创建多个实例。

<!--     scope属性:用来配置作用域,缺省值是singleton(即一个bean只创建一个实例),    如果值为prototype(即一个bean会创建多个实例)。 --><bean id="s1" class="scope.ScopeBean" scope="prototype"/>

生命周期

初始化 分配资源。

销毁 释放资源。

初始化方法:

使用init-method属性来指定初始化方法名。

注:spring容器创建对象之后,会立即调用初始化方法。

销毁方法:

使用destroy-method属性来指定销毁方法名。

注:spring容器在关闭之前,会先销毁对象,在销毁对象之前,会先调用对象的销毁方法。

只用作用域为单例时,销毁方法才会执行。

<!--     init-method属性:用来指定初始化方法。    destroy-method属性:用来指定销毁方法。    lazy-init属性:指定是否默认加载,如果值为true,表示延时加载。 --><bean id="mb1" class="scope.MessageBean" init-method="init" destroy-method="destroy" lazy-init="true"/>

延迟加载(了解)

spring容器启动之后,会将所用作用域为单例的bean创建好。

指定lazy-init属性值为true,此时,spring容器对于作用域为单例的bean,就不会创建相应的实例了。

原创粉丝点击