• 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. 一、换系统wince ---到 linux ubuntu 桌面

    今天终于拿到了OK6410,还是比较满意.预装的为wince系统,现要修改为linux 按照  OK6410-A开发板LINUX3.0.1-2013-01用户手册 进行 1.遇到问题.无法写入SD卡( ...

  2. Unity3d之Mecanim(新版动画系统)

    1,动画系统配置,2,代码控制动画 原文地址:http://blog.csdn.net/dingkun520wy/article/details/51247491 1,动画系统配置 创建Animato ...

  3. 避免写出IE Bug

    https://github.com/KitWong/KitWong.github.io/tree/master/_posts

  4. linux查看端口是否已开启和查看文件数

    查看端口是否开启 lsof -i:80 查看文件数 ls | wc -w

  5. 20145120 《Java程序设计》实验二实验报告

    20145120 <Java程序设计>实验二实验报告 实验名称:Java面向对象程序设计 实验目的与要求: 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握U ...

  6. htop

    apt-get install htop

  7. solr简易安装配置

    之前弄了段时间的lucene,昨天下午开始学solr,准备用到项目中,在网上找了一些教程,有的不是讲得太复杂,就是讲得不在点上,花了不少冤枉时间.有的一上来就花过半的篇幅大讲特讲“3H”,(what, ...

  8. sql语句not in判断条件注意事项

    sql语句not in判断条件注意事项 问题描述:mysql数据库,存在两个表org表和kdorg表,用于存储组织信息.现在我需要从org表找出组织,条件为该组织不在kdorg表里. sql语句:se ...

  9. 在eclipse中配置maven

    http://pansanday.blog.163.com/blog/static/381662802012727103454743/ 从eclipse 3.6开始,eclipse有一个marketp ...

  10. DepthClipEnable error

    刚刚呢又遇到这种鬼扯的问题,ps就return个(1,1,0,1) nisight的汇编都写对了,但结果就是画不出任何东西,按照经验,必然是某个state.... 我就找啊找啊,被我找到一个 Rast ...