Spring学习笔记(一)

学完了hibernate框架,再来接着学习Sping框架。
看了一些Spring介绍,都说Spring是非常牛的一个框架,它属于无侵入式的设计,也就是说具体编程的时候不必以来于Spring的API,并且它内部有SpringMVC,因此可以替代SSH中的Struts,而ormapping的框架也可以替换,唯有Spring这个平台无可替代。另一方面,Spring在运行的时候可是使程序员感觉仿佛没有使用框架,也就是对成员来说是透明的。总之就是一个字:牛。

一、Spring的IoC

Spring设计的核心就是它的IoC(控制反转)和DI(依赖注入),今天先记下关于IoC的内容。IoC用一句话来解释就是:从前需要程序员自己创建的对象现在可以由Spring框架完成创建。那么先来感受一下Spring的运行过程吧。我们来写一个HelloWorld的例子,一共需要三步。
工程的目录结构如下:
Spring学习笔记(一)_第1张图片
我们需要首先导入两个jar包:spring.jar和commons-logging.jar
第一步,写一个文件叫做:HelloWorld.java,代码如下:

package com.tt.helloworld;

import java.io.Serializable;

public class HelloWorld implements Serializable {

    public HelloWorld(){
        System.out.println("create object by constructor");
    }
    public void hello(){
        System.out.println("hello world");
    }
}

第二步,写一下配置文件: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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


        <bean id="helloWorld" class="com.tt.helloworld.HelloWorld"></bean>
        </bean>

</beans>

这样一来,Spring容器就可以帮我们创建这个HelloWorld对象了。从配置文件中可以看出我使用的是2.5版本的Spring。
第三步,写一个测试类来测试一下:

package com.tt.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tt.helloworld.HelloWorld;

public class TestHelloWorld {
    @Test
    public void Test(){
    //这一行代码启动Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //这一行代码获取对象
        HelloWorld helloworld = (HelloWorld) context.getBean("hello");
        helloworld.hello();
    }

}

这样我们就可以看到输出的结果了:

create object by constructor hello world

诶,为什么输出了两句话呢?因为Spring容器默认是使用无参构造方法创建对象的,我在无参构造方法中写了System.out.println("create object by constructor"); 于是就多输出一行。

二、别名

在获取对象的时候可以使用别名,也就是第二个名字。使用方法就是在配置文件中加一句话:

<alias name="helloWorld" alias="hello"/>

还是用HelloWorld来举例子,当写了这句话之后,我们使用context.getBean()的时候就可以使用alias后面的值了。

三、对象的创建方式

(1)默认通过无参的构造方法,这个在前面已经有所体现。
(2)静态工厂:
首先,写一个工厂方法:HelloWorldFactory

package com.tt.helloworld;

public class HelloWorldFactory {
    public static HelloWorld getInstance(){
        return new HelloWorld();
    }
}

然后在配置文件中进行配置:

<bean id="helloWorldFactory" class="com.tt.helloworld.HelloWorldFactory" factory-method="getInstance">
</bean>

然后在测试类中进行测试:

@Test
    public void TestFactory(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloworld = (HelloWorld) context.getBean("helloWorldFactory");
        helloworld.hello();
    }

这样就可以通过HelloWorld helloworld = (HelloWorld) context.getBean("helloWorldFactory");来获取HelloWorld对象了。
(3)实例工厂创建,这种方法由于使用频率不高,就不介绍了。

四、对象创建的时机

(1)启动Spring的时候就创建对象,即执行ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");这一行代码的时候就创建对象。
(2)调用context.getBean()的时候才创建,这种方式只需要一个配置,即在<bean>中加入lazy-init="true" 这一属性即可。
一般情况下推荐使用第一种方式,因为这种方式在web应用服务器启动的时候就创建对象,如果有错误就会及早发现,而采用第二种方式发现错误较晚。当然,如果应用包含非常多的数据,为了节省空间,可以使用第二种方式。

五、对象创建的范围

在配置文件中有一个属性叫做:scope,若将其配置为singleton或者不配置这个属性,对象会是单例的,因为框架默认创建对象单例,若配置为prototype则会是多例的,每次创建的对象都不同。

今天先总结到这里,之后再继续总结。所有的成功都与坚持有关,加油。

你可能感兴趣的:(spring)