什么是spring

spring是一个开源的轻量级的应用开发框架,它提供了IOC和AOP应用,可以减少组件之间的耦合度,即

解耦,spring容器可以创建对象并且管理对象之间的关系。

一:实例化spring容器对象

1:导入spring相关支持jar包

2:创建spring容器的配置文件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:jdbc="http://www.springframework.org/schema/jdbc"
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:util="http://www.springframework.org/schema/util"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> </beans>

3:创建TestCase测试类,并引入Junit包

 /**
*
*/
package com.hlcui.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Administrator
*
*/
public class TestCase {
@Test
/**测试实例化spring容器对象*/
public void testInitContext() {
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
System.out.println("spring容器对象:" + ac);
}
}

4:运行测试类,得到spring容器对象

二:使用spring容器创建javabean对象

1:在spring容器配置文件applicationcontext.xml中定义创建bean的声明

其中id代表bean的名称,也可以使用name属性名,id在配置文件中是唯一的,不可以重复,它就是给bean取一个名字,

class代表类,默认情况下spring会自动调用类无参构造方法创建对象,factory-method:工厂方法,类会调用工厂方法

常见对象,第三种情况是先通过java.util.GregorianCalendar无参构造创建calendar对象,然后通过调用calendar的

getTime方法创建date对象

 <!-- 使用构造器实例对象 -->
<bean id="cal1" class="java.util.GregorianCalendar"></bean> <!-- 使用静态工厂方式实例对象 -->
<bean id="cal2" class="java.util.Calendar" factory-method="getInstance"></bean> <!-- 使用实例工厂方法创建对象 -->
<bean id="cal3" class="java.util.GregorianCalendar"></bean>
<bean id="date1" factory-bean="cal3" factory-method="getTime"></bean>

2:在TestCase测试类中,创建方法测试实例对象

 /** 获取spring容器对象 */
public ApplicationContext getApplicationContext() {
String conf = "applicationContext.xml";
return new ClassPathXmlApplicationContext(conf);
}

spring容器对象调用getBean()方法,其中第一个参数是bean的id属性值,第二个参数是得到对象的类型

 @Test
/**测试spring容器使用构造器实例对象*/
public void testGregorianCalendar() {
ApplicationContext ac = getApplicationContext();
Calendar cal1 = ac.getBean("cal1", Calendar.class);
System.out.println("cal1:" + cal1); Calendar cal2 = ac.getBean("cal2", Calendar.class);
System.out.println("cal2:" + cal2); Date date = ac.getBean("date1", Date.class);
System.out.println("date:" + date); }

3:运行测试类,得到结果如下:

三:控制bean的实例化

1:bean对象的创建模式

a.创建ExampleBean对象

 /**
*
*/
package com.hlcui.dao; /**
* @author Administrator
*
*/
public class ExampleBean { public ExampleBean() {
System.out.println("实例化ExampleBean对象...");
} public void execute() {
System.out.println("执行ExampleBean方法...");
}
}

b.在spring容器配置文件中配置ExampleBean的bean

 <!-- 实例化ExampleBean对象 -->
<bean id="exampleBean" class="com.hlcui.dao.ExampleBean"></bean>

c.在TestCase测试类中添加测试方法

 @Test
/**
* 测试创建Bean对象的模式
* 比较两次创建的是否是同一个对象,如果为true,则是同一个对象,如果为false则不是
*/
public void testCreateExampleBean() {
ApplicationContext ac = getApplicationContext();
ExampleBean eb1 = ac.getBean("exampleBean", ExampleBean.class);
ExampleBean eb2 = ac.getBean("exampleBean", ExampleBean.class);
System.out.println(eb1 == eb2);
}

运行测试类,结果如下:

可以看出spring容器默认实例化对象是单例模式,只创建一个对象。

d.如果修改配置,在bean中添加属性 scope="prototype"

 <!-- 实例化ExampleBean对象 -->
<bean id="exampleBean" class="com.hlcui.dao.ExampleBean" scope="prototype"></bean>

在运行测试类结果为:

可以看出创建了两个对象,如果设置scope为原型模式,则不是单例模式。

2:Bean对象的初始化和销毁

a.在ExampleBean类中添加init()方法和destroy()方法

 /**
*
*/
package com.hlcui.dao; /**
* @author Administrator
*
*/
public class ExampleBean { public ExampleBean() {
System.out.println("实例化ExampleBean对象...");
} public void execute() {
System.out.println("执行ExampleBean方法...");
} public void init() {
System.out.println("初始化ExampleBean对象...");
} public void destroy() {
System.out.println("销毁ExampleBean对象...");
}
}

b.在spring配置文件中ExampleBean对应的bean元素上添加属性init-method="init"

 <!-- 实例化ExampleBean对象 -->
<bean id="exampleBean" class="com.hlcui.dao.ExampleBean"
init-method="init" scope="prototype"></bean>

c.运行测试方法,结果如下:

可以看出调用了init()方法

如果想在spring容器销毁时调用destroy()方法,只需在配置文件bean元素上添加

属性destroy-method()

 <!-- 实例化ExampleBean对象 -->
<bean id="exampleBean" class="com.hlcui.dao.ExampleBean"
init-method="init" destroy-method="destroy" scope="prototype"></bean>

测试方法中,将容器对象强制成AbstractApplicationContext对象,因为该对象定义了close()方法。

注:如果父类中的方法不能够满足业务要求时,可以将父类强制为子类,因为子类中拥有更多的方法。

 @Test
/**
* 测试创建Bean对象的模式
* 比较两次创建的是否是同一个对象,如果为true,则是同一个对象,如果为false则不是
*/
public void testCreateExampleBean() {
ApplicationContext ac = getApplicationContext();
ExampleBean eb1 = ac.getBean("exampleBean", ExampleBean.class);
ExampleBean eb2 = ac.getBean("exampleBean", ExampleBean.class);
System.out.println(eb1 == eb2); /**将容器对象强制为AbstractApplicationContext对象,因为该对象定义了close方法*/
AbstractApplicationContext aac = (AbstractApplicationContext) (ac);
aac.close();
}

运行测试类,结果如下:

发现并没有调用销毁对象的destroy方法,这里需要注意,destroy方法,只有单例模式

下才会调用,这里是原型模式,所以在配置文件中将scope="prototype" 修改为

scope="singleton"

 <!-- 实例化ExampleBean对象 -->
<bean id="exampleBean" class="com.hlcui.dao.ExampleBean"
init-method="init" destroy-method="destroy" scope="singleton"></bean>

这是再运行测试方法:

发现在卸载容器时,会调用destroy()方法。

d:如果将default-init-method="init"以及default-destroy-method="destroy"放到顶级的beans元素上,

那么它会作用于所有的bean对象。

3:bean对象的创建时机

a:注释掉一下代码

 @Test
/**
* 测试创建Bean对象的模式
* 比较两次创建的是否是同一个对象,如果为true,则是同一个对象,如果为false则不是
*/
public void testCreateExampleBean() {
ApplicationContext ac = getApplicationContext();
/*ExampleBean eb1 = ac.getBean("exampleBean", ExampleBean.class);
ExampleBean eb2 = ac.getBean("exampleBean", ExampleBean.class);
System.out.println(eb1 == eb2); *//**将容器对象强制为AbstractApplicationContext对象,因为该对象定义了close方法*//*
AbstractApplicationContext aac = (AbstractApplicationContext) (ac);
aac.close();*/
}

运行测试类:

说明spring容器对象一旦创建,那么bean对象就会创建。

b:将配置文件中bean元素上添加lazy-init="true"属性

     <!-- 实例化ExampleBean对象 -->
<bean id="exampleBean" class="com.hlcui.dao.ExampleBean"
lazy-init="true" init-method="init" destroy-method="destroy" scope="singleton"></bean>

那么在运行测试方法时,并没有实例任何对象,说明延迟了实例bean对象。

c:去掉注释中的代码,再运行测试方法时

说明在使用到ExampleBean对象的时候才会实例化对象

如果在顶级元素beans上面添加属性default-lazy-init="true"时,那么默认在实例所有的bean

对象的时候都会延迟。

注:这里有点类似servlet容器,如果在web.xml中配置loadon-start-up时,那么在服务启动时就会创建

servlet对象,等待浏览器客户端的请求,如果不配置,那么只有当第一个请求发送过来的使用,才会创建

servlet对象,而且只会创建一个servlet对象。

<!-- 实例化ExampleBean对象 --><bean id="exampleBean" class="com.hlcui.dao.ExampleBean"init-method="init" destroy-method="destroy" scope="prototype"></bean>

Spring学习总结一——SpringIOC容器一的更多相关文章

  1. Spring学习总结四——SpringIOC容器四

    一:spring容器给bean对象注入属性值 1:注入基本属性值 a. 创建MessageBean类: /** * */ package com.hlcui.dao; /** * @author Ad ...

  2. Spring学习总结三——SpringIOC容器三

    一:spring容器自动装配注入 为了减少xml中配置内容,可以使用自动装配注入,代替setter注入,只需要在 bean对象配置中添加属性autoWire即可,那么在类中就会自动扫描setXXX() ...

  3. Spring学习总结二——SpringIOC容器二

    一:指定bean的依赖关系 例如examplebean对象依赖examplebean1对象,那么在创建examplebean对象之前就 需要先创建examplebean1对象. 1:创建Example ...

  4. Spring学习总结五——SpringIOC容器五

    一:spring组件扫描 可以使用注解的方式,代替在xml配置文件配置bean,可以减少配置文件的书写,只需要在spring容器配置 文件中配置<context:component-scan b ...

  5. 三、spring成长之路——springIOC容器详解(上)

    目录 一.springIOC 一.springIOC 控制反转和依赖注入: ​ 简单的说就是将对象的创建,属性的的设置交给spring容器进行管理,而不再由用户自己创建,当用户需要使用该接口或者类的时 ...

  6. 四、spring成长之路——springIOC容器(下)

    目录 5.spring注解开发(Spring扩展知识) 5.1定义配置类:@Configuration 声明一个类为IOC容器 @Bean定义一个Bean 5.2.按照条件进行注入 5.3.@Impo ...

  7. Spring学习之旅(二)--容器

    在 Spring 应用中,所有的对象都在 Spring 容器(container) 里,容器负责对象的创建.配置.装配并管理它们的整个生命周期. Spring 容器 Spring 容器 并不是只有一个 ...

  8. Spring学习记录1——IoC容器

    IoC容器 1.1  IoC概述 Ioc(Inverse of Control,控制反转)是Spring容器的内核.对于软件来说,即某一接口具体实现类的选择控制权从调用类中移除,转交给第三方决定,即由 ...

  9. Spring学习一: Ioc容器

    Spring 容器:      Spring 容器是Spring框架的核心.Spring容器将创建Bean对象实例,把它们联系在一起,配置它们,并管理它们整个生命周期从创建到销毁.Spring 容器通 ...

随机推荐

  1. 创建可执行的JAR包

    创建可执行的JAR文件包,需要使用带cvfm参数的jar命令,命令如下:JAR cvfm test.jar manifest.mf testtest.jar和manifest.mf为两个文件,分别对应 ...

  2. Generic【Pluralsight】

    prepare Employee Structure namespace CollectIt { public class Employee { public string Name { get; s ...

  3. Oracle 触发器在日志管理开发中的应用

    摘要: 本文讨论了利用数据库中的触发器对日志管理进行设计与实现的方法, 是对原来在客户端软件中编写日志管理方法的一种改进, 并给出了 Oracle9i 中的实例演示.关键词: Oracle; 触发器; ...

  4. windows无法搜索新更新 80072ee2

      http://windows.microsoft.com/zh-cn/windows/windows-update-error-80072ee2#1TC=windows-7    

  5. Class hierarchy of UIResponder as well as subclasses of UIView and UIControl

    When you were dragging in your label and your button to this view, you were adding them as subviews. ...

  6. Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题

    B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...

  7. 007_尚学堂_高明鑫_android 之项目的打包apk与apk的反编译

    http://www.tudou.com/programs/view/kMQlxff8evM/

  8. .Net 下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) [轉]Redfox

    一直以来,我都为动态调用WebService方法而烦恼.在.Net环境下,最常用的方法就是采用代理类来调用WebService,可以通过改变代理类的Url属性来实现动态调用,但当xmlns改变时就会出 ...

  9. 【JavaScript】关于prototype原型的一些链接

    http://www.cnblogs.com/slowsoul/archive/2013/05/30/3107198.html http://www.thinksaas.cn/group/topic/ ...

  10. [AngularJS] Accessing Scope from The Console

    Using Angular, you can actually access the scope and other things from the console, so when you have ...