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 容器通 ...
随机推荐
- Web服务器与Web系统发布
在讨论Web系统发布之前,我们先来辨析两个概念:服务器.Web服务器. 通常,我们说的服务器,是一台提供服务的计算机,是硬件概念.这台主机有其IP地址,有服务端口,我们要访问时,就是通过IP地址唯一地 ...
- android开发中提示:requires permission android.permission write_settings解决方法
一.在Manifest.xml 中添加: <uses-permission android:name="android.permission.WRITE_CONTACTS" ...
- jQuery(一)delegate() 方法
定义和用法 delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数. 使用 delegate() 方法的事件处理程序适用于当前或未来 ...
- github 多帐户使用
同一台电脑有2个github账号?咋办 比如一个公司账号一个个人账号. 私人账号如下: 邮箱example@126.com 账号:example 公司工作账号如下: work@xx.com 账号:my ...
- 试读《JavaScript语言精髓与编程实践》
有幸看到iteye的活动,有幸读到<JavaScript语言精髓与编程实践_第2版>的试读版本,希望更有幸能完整的读到此书. 说来读这本书的冲动,来得很诡异,写一篇读后感,赢一本书,其实奖 ...
- Codeforces Round #180 (Div. 2) C. Parity Game 数学
C. Parity Game 题目连接: http://www.codeforces.com/contest/298/problem/C Description You are fishing wit ...
- Java虚拟机的启动与程序的执行
这篇文章是从 OpenJDK 源码的角度讲当我们执行了 java -classpath . hello 之后,java.exe 怎样从 main 函数開始运行,启动虚拟机,并运行字节码中的代码. 实验 ...
- 【JavaScript】新浪微博ajax请求后改变地址栏url,但页面不跳转的方案解析
新浪微博当你弹出一个视频的时候再点下一页时,原视频还在,而且地址栏的url的页数变了.对于这种网上讨论最多的方案有以下几种: 一.通过锚点Hash实现在这方面其实国内很早就有做了,比如淘宝画报,通过的 ...
- wordpress文章ID不连续显示问题的完美解决
在最新版的 wordpress 系统中,依然存在着文章ID不连续显示的问题,也就是我们还没有上传多少文章,在数据库里的ID号已经很大了,也就是说如果我们的博客使用的是固定链接,那么在前台显示的ID相差 ...
- Swift之UIBezierPath
使用UIBezierPath可以创建基于矢量的路径.使用此类可以定义简单的形状,如椭圆.矩形或者有多个直线和曲线段组成的形状等.主要用到的该类的属性包括 moveToPoint: //设置起始点 ad ...