• 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. WPF——数据绑定(一)什么是数据绑定

    注意:本人初学WPF,文中可能有表达或者技术性问题,欢迎指正!谢谢! 一:什么是数据绑定? “Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简 ...

  2. canvas画时钟

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  3. 转载:centos上yum安装apache+php+mysql等

    1. 更新系统内核到最新. [root@linuxfei ~]#yum -y update 系统更新后,如果yum安装时提示错误信息,请执行以下命令修复. [root@linuxfei ~]#rpm ...

  4. Netsharp快速入门(之2) 基础档案(之A 创建插件和资源)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 第三章     基础档案开发 本文不再对此需求进行分析设计,其实分析设计的结果在下文会体现在平台的使用过程中,这个销售系统分成两个模 ...

  5. spring--注入类型--构造方法(不常用)

    3.3.1.1. Constructor Injection Constructor-based DI is effected by invoking a constructor with a num ...

  6. JSP页面动态联动

    效果如图: 页面用法: body部分: 注意:控制层Controller中:

  7. 生成中文版JavaDoc

    RT. 在用IDEA生成中文版JavaDoc时,出现如下问题: java.lang.IllegalArgumentException at sun.net.www.ParseUtil.decode(P ...

  8. Visual Studio快捷键设置

    1.查看当前快捷键:环境-键盘-按快捷键2.文本编辑器-C#-显示-行号3.文本编辑器-C#-制表符-插入空格4.文本编辑器-所有语言-没有选定内容时对空行应用剪切或复制命令5.Ctrl+Shift- ...

  9. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

  10. ActionScript基本语法讲解

    var a:int = 3;var b:int = 4; b = 9; trace ("a的值为:"+a);trace ("b的值为:"+b); var x1: ...