码迷,mamicode.com
首页 > 编程语言 > 详细

Spring基础

时间:2015-05-30 11:59:45      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

1.Spring的jar包
    核心类库:disk\spring.jar
    第三方类库:lib\jakarta-commons\commons-logging.jar
    
    如果使用了切面编程(AOP),还需要下列jar文件 
        lib/aspectj/aspectjweaver.jar和aspectjrt.jar
        lib/cglib/cglib-nodep-2.1_3.jar
    如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件 
        lib\j2ee\common-annotations.jar
        注:JSR(Java 规范请求)是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR(Java 规范请求),以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准

 

2.Spring配置文件
    applicationContext.xml

 

3.Spring基本功能详解

3.1 SpringIOC Spring的控制反转:把对象的创建、初始化、销毁等工作交给spring容器来做。由spring容器控制对象的生命周期 1>启动Spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 2>从spring容器中提取对象 Persn person = (Person) applicationContext.getBean("person");
3.2别名 <bean id="person" class="com.sn.domain.Person"></bean> <alias name="person" alias="哈哈"/>
3.3Spring容器内部对象
3.3.1创建对象的三种方式
1.无参构造函数 <bean id="person" class="com.sn.domain.Person"></bean> ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) applicationContext.getBean("person"); person.say(); 2.静态工厂 public class PersonFactory { public static Person getIntance(){ return new Person(); } } <bean id="personFactory" class="com.sn.test.PersonFactory" factory-method="getIntance"></bean> ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) applicationContext.getBean("personFactory"); person.say(); 3.实例工厂 public class PersonFactory2 { public Person getIntance(){ return new Person(); } } <bean id="personFactory2" class="com.sn.test.PersonFactory2"></bean> <bean id="personFactory3" factory-bean="personFactory2" factory-method="getIntance"></bean> ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) applicationContext.getBean("personFactory3"); person.say();
3.3.2对象的scope 1.singleton(默认值) ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) applicationContext.getBean("person"); System.out.println(person); person = (Person) applicationContext.getBean("person"); System.out.println(person); 打印结果: com.sn.test.Person@126f827 com.sn.test.Person@126f827 在每个Spring IoC容器中一个bean定义只有一个对象实例(共享)。 默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如: <bean id="person" class="cn.sn.Person" lazy-init="true"/> 如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下: <beans default-lazy-init="true“ ...> 2.prototype 允许bean可以被多次实例化(使用一次就创建一个实例), Spring不能对一个prototype bean的整个生命周期负责, 这就意味着清楚prototype作用域的对象并释放任何prototype bean所持有的昂贵资源都是客户端的责任 3.Request 在一次HTTP请求中,一个bean定义一个实例,即每次HTTP请求将会有各自的bean实例,他们依据某个bean定义创建而成,该作用于仅在基于web的Spring ApplicationContext庆幸下有效 4.Session 在一个HTTP Session中,一个bean定义对应一个实例,该作用域仅在基于web的Spring ApplicationContext情形下有效 5.Global session 在一个全局的HTTP Session中,一个bean定义对应一个实例,典型情况下,仅在使用portletcontext的时候有效,该作用域仅在基于web的Spring applicationContext情形下有效

  3.3.3
初始化bean时机 Spring默认在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初始化的一部分,ApplicationContext会自动创建并配置所有的singleton bean.通常情况下这是件好事。因为这样在配置中有任何错误能立即发现。 Lazy-init=”true or false” Lazy-init 为false,spring容器将在启动的时候报错(比较好的一种方式) Lazy-init 为true,spring容器将在调用该类的时候出错
3.3.4 init、destroy Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法。 <bean id=“foo” class=“...Foo” init-method=“init” destory-method=“destroy”/> 当foo被载入到Spring容器中时调用init-method方法。当foo从容器中删除时调用destory-method(scope = singleton有效)

 

Spring基础

标签:

原文地址:http://www.cnblogs.com/jsnan/p/4539986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!