从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别:

具体案例如下

结论:

1、如果使用上下文ApplicationContext,则配置的bean如果是Singleton不管你用不用,都被实例化(好处是可以预先加载,用时就不加载,速度快,缺点就是耗内存)

2、如果是BeanFactory,当你实例化该对象时候,配置的bean不会被马上实例化,当你使用的时候才实例(好处是节约内存,缺点就是速度有点慢)

3、规定:一般没有特殊要求,应当使用ApplicationContext完成(90%),当内存确实太小时可以用beanFactory,比如移动设备或手持设备。

项目结构:

beans.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="student" class="com.litao.ioc.Student">
<property name="name" value="小猪" />
</bean>
</beans>

Student.java

package com.litao.ioc;

public class Student {

	private String name;
//Java对象都有一个默认无参构造函数
public Student(){
System.out.println("对象被创建");
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

App1.java

package com.litao.ioc;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource; public class App1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub //从ApplicationContext中取bean
//ApplicationContext ac = new ClassPathXmlApplicationContext("com/litao/ioc/beans.xml");
//当我们去实例化beans.xml,该文件中配置的bean被实例化(),如果该bean scope是singleton不管你有没有
//使用这个bean,该bean都会被实例化.
//从bean工厂取出Student //如果我们使用BeanFactory去获取bean,当你只是实例化该容器,那么容器的bean不会被实例化
//只有当你去使用getBean某个bean时,才会实时被创建 BeanFactory factory = new XmlBeanFactory(
new ClassPathResource("com/litao/ioc/beans.xml"));
//运行时没有Student被创建
factory.getBean("student"); //软件发展速度是慢于这个硬件的 } }

Spring框架学习之第4节的更多相关文章

  1. Spring框架学习之第2节

    传统的方法和使用spring的方法 使用spring,没有new对象,我们把创建对象的任务交给了spring的框架,通过配置用时get一下就行. 项目结构 applicationContext.xml ...

  2. Spring框架学习之第1节

    spring快速入门 ①   spring是什么? Struts是web框架(jsp/action/actionform) hibernate是orm框架(对象和关系映射框架),处于持久层 sprin ...

  3. Spring框架学习之第9节

    aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...

  4. Spring框架学习之第8节

    <bean id=”foo” class=”…Foo”> <property name=”属性”> <!—第一方法引用--> <ref bean=”bean对 ...

  5. Spring框架学习之第3节

    model层(业务层+dao层+持久层) spring开发提倡接口编程,配合di技术可以更好的达到层与层之间的解耦 举例: 现在我们体验一下spring的di配合接口编程,完成一个字母大小写转换的案例 ...

  6. Spring框架学习之第7节

    配置Bean的细节 ☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大 ②如何给集合类型注入值 Java中主要的map,set,list / 数组 ...

  7. Spring框架学习之第6节

    bean的生命周期 为什么总是一个生命当做一个重点? Servlet –> servlet生命周期 Java对象生命周期 往往笔试,面试总喜欢问生命周期的问题? ①   实例化(当我们的程序加载 ...

  8. Spring框架学习之第5节

    request session global-session 三个在web开发中才有意义 如果配置成prototype有点类似于request 如果配置成singleton有点类似于web开发中的gl ...

  9. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

随机推荐

  1. 11.Warning (332060): Node: pi_fck3p was determined to be a clock but was found without an associated clock assignment.

    解释及措施:(1):这个信号是不是你期望的时钟信号?还是被综合器误将普通信号综合成了时钟信号?有没有在代码中用过这个信号的上升沿/下降沿? (2):如果是期望的时钟信号,那么是否有可能调整管脚位置约束 ...

  2. Java BigDecimal大数字操作

    在java中提供了大数字的操作类,即java.math.BinInteger类和java.math.BigDecimal类.这两个类用于高精度计算,其中BigInteger类是针对大整数的处理类,而B ...

  3. 软件工程随堂小作业——随机四则运算Ⅱ(C++)

    一.设计思路 设计思路已给出,此处不再赘述. 二.源代码 (1)四则运算2.cpp(源文件) // 四则运算2.cpp : Defines the entry point for the consol ...

  4. C#使用Socket登陆WordPress源码

    就在昨晚,在本屌丝刚刚发布屌丝与女神的回忆史<C#外挂QQ找茬辅助源码,早期开发>后,在苏飞大哥的技术讨论群有个群友提出一个问题.使用http协议模拟工具可以登录成功Wordpress但是 ...

  5. 十个优秀的C语言学习资源推荐

    学习C语言,需要一点一滴,沉下心来,找个安静的地方,泡上一杯咖啡,在浓郁的香味中一起品味她.-- Boatman Yang 人们通常认为计算机编程很烦,但是有些人却从中发现了乐趣.每一个程序员不得不跟 ...

  6. AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet控件在C#中的引用

    这几天要是用AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet控件做查询,发现一系列问题,一点点记录下来吧,以备后查: 第一.相关属性:http://www. ...

  7. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  8. 【Convert Sorted Array to Binary Search Tree】cpp

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  9. NSIS中判断.net framework 是否安装

    在网上找了几个脚本,原理都是读取注册表进行判断,都可以用. 1.CheckDotNetFramework /* * Name: CheckDotNetFramework.nsh * Version: ...

  10. 重新认识Box Model、IFC、BFC和Collapsing margins

    尊重原创,转载自: http://www.cnblogs.com/fsjohnhuang/p/5259121.html 肥子John^_^ 前言   盒子模型作为CSS基础中的基础,曾一度以为掌握了I ...