Spring学习总结一——SpringIOC容器一
什么是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容器一的更多相关文章
- Spring学习总结四——SpringIOC容器四
一:spring容器给bean对象注入属性值 1:注入基本属性值 a. 创建MessageBean类: /** * */ package com.hlcui.dao; /** * @author Ad ...
- Spring学习总结三——SpringIOC容器三
一:spring容器自动装配注入 为了减少xml中配置内容,可以使用自动装配注入,代替setter注入,只需要在 bean对象配置中添加属性autoWire即可,那么在类中就会自动扫描setXXX() ...
- Spring学习总结二——SpringIOC容器二
一:指定bean的依赖关系 例如examplebean对象依赖examplebean1对象,那么在创建examplebean对象之前就 需要先创建examplebean1对象. 1:创建Example ...
- Spring学习总结五——SpringIOC容器五
一:spring组件扫描 可以使用注解的方式,代替在xml配置文件配置bean,可以减少配置文件的书写,只需要在spring容器配置 文件中配置<context:component-scan b ...
- 三、spring成长之路——springIOC容器详解(上)
目录 一.springIOC 一.springIOC 控制反转和依赖注入: 简单的说就是将对象的创建,属性的的设置交给spring容器进行管理,而不再由用户自己创建,当用户需要使用该接口或者类的时 ...
- 四、spring成长之路——springIOC容器(下)
目录 5.spring注解开发(Spring扩展知识) 5.1定义配置类:@Configuration 声明一个类为IOC容器 @Bean定义一个Bean 5.2.按照条件进行注入 5.3.@Impo ...
- Spring学习之旅(二)--容器
在 Spring 应用中,所有的对象都在 Spring 容器(container) 里,容器负责对象的创建.配置.装配并管理它们的整个生命周期. Spring 容器 Spring 容器 并不是只有一个 ...
- Spring学习记录1——IoC容器
IoC容器 1.1 IoC概述 Ioc(Inverse of Control,控制反转)是Spring容器的内核.对于软件来说,即某一接口具体实现类的选择控制权从调用类中移除,转交给第三方决定,即由 ...
- Spring学习一: Ioc容器
Spring 容器: Spring 容器是Spring框架的核心.Spring容器将创建Bean对象实例,把它们联系在一起,配置它们,并管理它们整个生命周期从创建到销毁.Spring 容器通 ...
随机推荐
- OpenSSL命令---ciphers
用途: 指令是用来展示用于SSL加密算法的工具.它能够把所有openssl支持的加密算法按照一定规律排列(一般是加密强度).这样可以用来做测试工具,决定使用什么加密算法. 用法: openssl ci ...
- Extjs 实用——不定时更新
用Ext做项目,总结的一些属性针对不同控件 1.grid 中,编辑表格内容时的点击变输入框 plugins: [cellEditing] //点击变输入框 columns:[{ //在需要变书入框的列 ...
- MyEclipse使用总结——使用MyEclipse打包带源码的jar包
平时开发中,我们喜欢将一些类打包成jar包,然后在别的项目中继续使用,不过由于看不到jar包里面的类的源码了,所以也就无法调试,要想调试,那么就只能通过关联源代码的形式,这样或多或少也有一些不方便,今 ...
- Android Studio 0.4 + PhoneGap 3.3 开发环境的搭建
最近在尝试HTML5移动平台下的开发 由于安卓的方便性,首先开始了安卓的试验 现在安卓下的开发工具首选 Android Studio (写这文章的时候,是0.4) 而跨平台的HTML5 App比较出名 ...
- 对Slony-I中wait on的理解
http://slony.info/documentation/2.1/advanced.html#AEN1425 4.1.2. Event Confirmations When an event i ...
- easy Html5 - Jquery Mobile之ToolBars(Header and Footer)
jquery 在web js框架上的风暴还在继续却也随着移动终端走向了mobile:那么jquery mobile到底包括些什么呢 简介工具栏是在移动网站和应用中的头部,尾部或者内容中的工具条:Jqu ...
- UVALive 4223 Trucking 二分+spfa
Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- 【JavaScript】Understanding callback functions in Javascript
Callback functions are extremely important in Javascript. They’re pretty much everywhere. Originally ...
- [Angular2 Form] Use RxJS Streams with Angular 2 Forms
Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...
- c#给用户控件添加事件处理程序
1.首先在usercontrol后台添加如下代码: public partial class MyControl: UserControl { //添加事件代理 public event ...