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 容器通 ...
随机推荐
- Java和MongoDB之Hello World
1.新建Project 新建Java Project,并把mongo-java-driver驱动加入到项目bulid path中,如果你使用的是maven增加依赖. <dependency> ...
- xiaocms 关于搜索功能 添加搜索字段
自己折磨了好几天 就是没研究个出像样的的东西 看了一下 core/controller/index.php searchAction()方法 但是不知从何下手.查了sql语句,还是没实现 请教了一位自 ...
- Linux之sed,awk(流编辑器)
sed: s----substitute(替换) 1. 文本替换(使用-i选项,可以将结果应用于原文件) many people在进行替换之后,借助重定向来保存文件(未使用-i选项): $ sed ...
- Server-U与IIS端口占用问题解决
在新版的ftp软件Server-U(11.X)中,由于其默认设置中监听了80端口,经常会导致IIS不能服务不能正常启用,下面记录如何修改server-u的80端口. 打开Server-U软件, ...
- 如何加入自定义WebControl
http://www.screencast.com/users/Dennis.Garavsky/folders/Default/media/c75b4ec6-1641-4f82-936e-39360d ...
- sc7731 Android 5.1 LCD驱动简明笔记之一
基于展讯sc7731 - Android 5.1 代码分析浏览.将屏蔽细节,把握整体,并且不涉及其他设备和LCD的交互. 以下对sc7731 lcd大体流程进行简要说明. 第一,lcd 的两个阶段 1 ...
- JavaScript的角色巨变和Web技术的发展
曾经JavaScript是职业程序员看不上眼的脚本语言,如今只有高级程序员才能驾驭它. JavaScript性质和地位的天翻地覆,正是Web技术飞速变化的印证. 最初职业程序员轻视JavaScript ...
- 博客中最快捷的公式显示方式:Mathjax + Lyx
经常为在博客园中显示公式而烦恼的同志们看过来!! 什么是mathjax? 答:就是在web中显示公式用的,基于JavaScript写的,关键是开源,网址http://www.mathjax.org/, ...
- case when遇到空串转成0
须要注意:假设字段为varchar类型,when后的条件要加上引號 SELECT (CASE marital_status WHEN 0 THEN '已婚' WHEN 1 THEN '未婚' ELSE ...
- plupload上传控件错误exec(this.uid, component, action, args)
plupload上传控件错误exec(this.uid, component, action, args) --undefined is not a function 原因:Flash元素隐藏后调用控 ...