Spring框架详解(1)

来源:互联网 发布:exe软件看源码 编辑:程序博客网 时间:2024/06/05 21:28

Spring 4.3.8

开发所需工具

Spring 核心功能(CoreContainer): spring-core, spring-beans, spring-context, spring- expression, commons-logging
spring-core 和spring-beans 提供了基本的依赖注入和控制反转功能
spring-context 在Core&Beans的基础上提供基本的框架API,ApplicationContext接口是 模块的核心.
spring-expression 提供了在运行时操作对象的语言

Spring 基本功能

IOC

Spring的控制反转:把对象的创建、初始化、销毁等工作交给spring容器来做。 由 spring容器控制对象的生命周期。
IoC : Inverse Of Control(控制反转)
1. 把自己 new 的东西改为由容器提供
a)初始化具体值
b)装配
2. 好处:灵活装配

Spring 容器内部对象

创建对象的方式

无参构造函数
 public class HelloWorld {    public  HelloWorld(){    System.out.println("spring 在默认的情况下,使用默认的构造函数");     }    public void sayHello(){        System.out.println("hello");    }}
    <!--一个 bean 就是描述一个类 id就是标示符        命名规范: 类的第一个字母变成小写,其他的字母保持不变        class为类的全名--><bean id="helloWorld" class="com.lanou.domain.HelloWorld"></bean>
//测试类 @Test    public void testHelloWorld() {        //启动spring容器        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        //从spring容器中把对象提取出来        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");        helloWorld.sayHello();    }
 /**     * 静态工厂     * 在spring内部,调用了HellowWorldFactory内部的getInstance内部方法     * 而该方法的内容就是创建对象的过程.是由程序员来完成的     */    @Test    public void testHellowWorldFactory(){        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorldFactory");        helloWorld.sayHello();    }
public class HelloWorldFactory2 {    //实例工厂    public HelloWorld getInstance(){        return new HelloWorld();    }}
 <!--实例工厂        工厂类创建对象        工厂类,实例方法,必须先创建工厂,再创建对象,用factory的实例方法-->    <bean id="helloWorld2" class="com.lanou.swan.action.HelloWorldFactory2"/>    <bean factory-bean="helloWorld2" factory-method="getInstance"/>
 /**     * 实例工厂     * 1. spring 容器创建了一个实例工厂的 bean     * 2. 该bean调用了工厂方法的 getInstance 方法产生对象     */    @Test    public void test2() {        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        HelloWorld helloWorld2 = (HelloWorld) context.getBean("helloWorld2");        helloWorld2.sayHello();    }

对象的scope

简单说就是对象在spring容器(IOC容器)中的生命周期,也可以理解为对象在spring容器中的创建方式。 目前,scope的取值有5种取值:

singleton(默认)
每个Spring IoC 容器中一个bean定义只有一个对象实例(共享).
此取值时表明容器中创建时只存在一个实例,所有引用此bean都是单一实例。此外, singleton类型的bean定义从容器启动到第一次被请求而实例化开始,只要容器不销毁 或退出,该类型的bean的单一实例就会一直存活,典型单例模式,如同servlet在web 容器中的生命周期。

prototype
允许bean可以被多次实例化(使用一次就创建一个实例) . Spring不能对一个prototype bean的整个生命周期负责. 无论lazy-init 的值是什么, 都在 context.getBean时才创建对 象.
spring容器在进行输出prototype的bean对象时,会每次都重新生成一个新的对象给请 求方,虽然这种类型的对象的实例化以及属性设置等工作都是由容器负责的,但是只 要准备完毕,并且对象实例返回给请求方之后,容器就不在拥有当前对象的引用,请 求方需要自己负责当前对象后继生命周期的管理工作,包括该对象的销毁。也就是 说,容器每次返回请求方该对象的一个新的实例之后,就由这个对象“自生自灭”,最典 型的体现就是spring与struts2进行整合时,要把action的scope改为prototype。

Request
再次说明 request,session和global session类型只适用于web程序,通常是和 XmlWebApplicationContext共同使用,作用域仅在基于web的Spring ApplicationContext 情形下有效。
request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前 HTTP request 内有效。当处理请求结束,request作用域的bean实例将被销毁。

Session
session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在 当前HTTP session内有效。

Global session
在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用 portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下 有效。

初始化 bean 的时机

    Spring 默认在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初 始化的一部分,ApplicationContext 会自动创建并配置所有的singleton bean. 通常情况下这是件好事。因为这样在配置中有任何错误能立即发现。
<!--lazy-init="true" 或 "false"    lazy-init 为false,spring容器将在启动时报错    lazy-init 为true,spring容器将在调用该类时出错--> <bean id="per1" class="com.lanou.domain.Person"/><bean id="per2" class="com.lanou.domain.Person" lazy-init="true"/>
@Test        public void testPerson_lazy_init_true(){        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        //person2 在 context.getBean()时才创建        Person person = (Person) context.getBean("per1");         Person person2 = (Person) context.getBean("per2");        }

如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy- init=“true“,如下:

<beans default-lazy-init="true" ...>

init, destroy

Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和 拆卸bean的时候调用bean的两个生命周期方法。
public class Person {    public void init(){         System.out.println("初始化");}    public void destroy(){         System.out.println("销毁");    } }
 <bean id="per1" class="com.lanou.domain.Person" init-method="init" destroy-method="destroy"/>

当 per1 被载入到Spring容器中时调用init-method方法。当 per1 从容器中删除时调用 destroy-method(scope = singleton有效)

/***在构造方法之后立即执行 init 方法,*如果 spring 执行 close 方法,关闭之后执行 destroy*/@Testpublic void testPerson_init_destroy() {    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");    Person person = (Person)context.getBean("per1");    //spring 容器关闭((ClassPathXmlApplicationContext) context).close();

别名

 <bean id="car" name="BMCar" class="com.lanou.domain.Car"/> <!-- name的属性值和id对应--><alias name="car" alias="QQ"/>
 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  Car car1 = (Car) context.getBean("car");   Car car2 = (Car) context.getBean("QQ");
//都是同一个对象System.out.println(car1);System.out.println(car2);
原创粉丝点击