Spring框架学习之第5节
- 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节的更多相关文章
- Spring框架学习之第2节
传统的方法和使用spring的方法 使用spring,没有new对象,我们把创建对象的任务交给了spring的框架,通过配置用时get一下就行. 项目结构 applicationContext.xml ...
- Spring框架学习之第1节
spring快速入门 ① spring是什么? Struts是web框架(jsp/action/actionform) hibernate是orm框架(对象和关系映射框架),处于持久层 sprin ...
- Spring框架学习之第9节
aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...
- Spring框架学习之第8节
<bean id=”foo” class=”…Foo”> <property name=”属性”> <!—第一方法引用--> <ref bean=”bean对 ...
- Spring框架学习之第3节
model层(业务层+dao层+持久层) spring开发提倡接口编程,配合di技术可以更好的达到层与层之间的解耦 举例: 现在我们体验一下spring的di配合接口编程,完成一个字母大小写转换的案例 ...
- Spring框架学习之第7节
配置Bean的细节 ☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大 ②如何给集合类型注入值 Java中主要的map,set,list / 数组 ...
- Spring框架学习之第6节
bean的生命周期 为什么总是一个生命当做一个重点? Servlet –> servlet生命周期 Java对象生命周期 往往笔试,面试总喜欢问生命周期的问题? ① 实例化(当我们的程序加载 ...
- Spring框架学习之第4节
从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...
- Spring框架学习一
Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...
随机推荐
- urllib3 ProxyManager
ProxyManager is an HTTP proxy-aware subclass of PoolManager. It produces a singleHTTPConnectionPool ...
- c++事件内核对象(event)进程间激活(转)
源出处:http://blog.csdn.net/richerg85/article/details/7538493 此文主要说明的是,c++中创建的一个事件内核对象可以在不同的程序(进程)间共用,也 ...
- Careercup - Google面试题 - 6283958983589888
2014-05-06 11:31 题目链接 原题: Find the k-th Smallest Element in Two Sorted Arrays. I followed the algori ...
- jquery实现select下拉框可输入+联想关联option
下面代码摘自http://www.oschina.net/question/96791_12832 <script language="javascript" src=&qu ...
- cas 登陆超时 解决方案
在配置文件ticketExpirationPolicies.xml中配置: <bean id="grantingTicketExpirationPolicy" class=& ...
- 研究AFIncrementalStore
一.增量存贮(AFIncrementalStore:NSIncrementalStore)在CoreData中所处位置
- XSS的原理分析与解剖(二)
0×01 前言: 上节(http://www.freebuf.com/articles/web/40520.html)已经说明了xss的原理及不同环境的构造方法.本期来说说XSS的分类及挖掘方法. ...
- GCD创建一个单例
1.+(id)shareInstance{ static ClassA *A=nil; static dispatch_once_t onceToken; dispatch_once(&onc ...
- WSDL
WSDL 彻底理解webservice SOAP WSDL WSDL 详解 http://www.cnblogs.com/hujian/p/3494064.html http://www.cnblog ...
- 自己写简单CoreDataManager封装对CoreData操作
关于CoreData的介绍太多,网上一搜大把全是,这里不介绍CoreData,直接上代码,注释写的很详细,应该很容易理解,暂时现做简单的增删该查,后面有时间再做修改完善. CoreDataManage ...