• request
  • session
  • global-session

三个在web开发中才有意义

如果配置成prototype有点类似于request

如果配置成singleton有点类似于web开发中的global-session

  • 三种获取ApplicationContext对象引用的方法

1、ClassPathXmlApplicationContext:从类路径中加载

2、FileSystemXmlApplicationContext:从文件系统加载

3、XmlWebApplicationContext:从web系统中加载

项目结构:

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" scope="prototype" 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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class App1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//通过类路径来获取
ApplicationContext ac = new ClassPathXmlApplicationContext("com/litao/ioc/beans.xml");
//通过文件路径来获取,通过相对路径或绝对路径
//ApplicationContext ac = new FileSystemXmlApplicationContext("beans.xml");
//当tomcat启动的时候会去加载
//ApplicationContext ac = new 3.XmlWebApplicationContext("");
Student s1 = (Student)ac.getBean("student");
Student s2 = (Student)ac.getBean("student");
System.out.println(s1+" "+s2);
//配置成prototype,如果不使用则不给你创建对象,每次会产生全新的对象,对象的内存地址不一样
//配置singleton,如果不使用则会给你创建一个,不管使用多少次都是同一个对象,内存地址一样 } }

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

  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框架学习之第4节

    从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...

  9. Spring框架学习一

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

随机推荐

  1. 《verilog数字系统设计教程》书评

    这本书的确是一本很经典的关于verilog语法和一些基本概念的书籍,后面的例子也很好,但是对于初学者来说,我们需要掌握的是语法和一些基本的概念. 刚一开始这本书的中文语法有点不是很通顺,但是越是往后, ...

  2. 89C51单片机定时器控制的流水灯

    /***************************************************Copyright: 2014-02-11.version1.0File name: timer ...

  3. apk 加密

    为了防止apk被轻易破解,想办法对java层的代码进行加密,防止反编译,代码混淆基本没什么效果,一般情况下我会对dex进行加密,通过动态加载的方法实现java层的代码尽量被隐藏,而动态加载的实现通过j ...

  4. Notes of the scrum meeting(12.5)

    meeting time:18:00~18:30p.m.,December 5th,2013 meeting place:3号公寓一层 attendees: 顾育豪                   ...

  5. json序列指定名称

    class jsonModel{ public string md5 { get; set; } public object report { get; set; } public string @v ...

  6. [译] TypeScript入门指南(JavaScript的超集)

    你是否听过 TypeScript? TypeScript 是 JavaScript 的超集,TypeScript结合了类型检查和静态分析,显式接口.TypeScript是微软的开源项目,它是由C#之父 ...

  7. 【POJ】【1821】Fence

    DP/单调队列优化 题意:k个人粉刷总长为n的墙壁(或者说栅栏?),每个人有一个必刷点s[i](这个人也可以一点也不刷,如果刷就必须刷这个点),最大粉刷长度l[i](必须是连续粉刷一段),和粉刷一格的 ...

  8. 【BZOJ】【1028】【JSOI2007】麻将

    暴力/模拟 $n\leq400$,嗯……这是一个很小的数据范围= = 判断一副牌是不是听牌并求出听什么牌太麻烦了,干脆我们直接判是不是胡牌好了~ 枚举胡的是哪张牌,然后判一下加上这张牌后是否能胡. 算 ...

  9. Java 函数参数传递方式详解 分类: Java Game 2014-08-15 06:34 82人阅读 评论(0) 收藏

    转:http://zzproc.iteye.com/blog/1328591 在阅读本文之前,根据自己的经验和理解,大家可以先思考并选择一下Java函数的参数传递方式:  A. 是按值传递的?  B. ...

  10. 异步解压ZIP文件

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...