Spring学习 Ioc篇一

      一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现。就从Ioc篇开始学习。

1、实例化spring容器的两种方法

      方法一:在类路径下寻找配置文件来实现Spring容器的实例化

ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});

     方法二:在文件系统路径下寻找配置文件进行Spring容器的实例化

ApplicationContext cx= new FileSystemXmlApplicationContext(new String[]{"D:\\applicationContext.xml"});

     在实际的应用中建议使用第一种方式,在程序开发中比较灵活,与路径无关。同时,sping可以指定多个配置文件,由数组传入。

2、实例化bean的三种方式

       方法一:使用类构造器实例化

      

//接口
package com.cvicse.service;
public interface PersonService {
	public void save();
}
//实现类
package com.cvicse.service.impl;
import com.cvicse.service.PersonService;
public class PersonServiceBean implements PersonService {
	public void save(){
		System.out.println("我是save()方法");
	}
}

    在applicationContext.xml的XML中配置如下:

 <bean id="personService" class="com.cvicse.service.impl.PersonServiceBean"></bean>

   在应用类中调用

ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
PersonService personService = (PersonService)cx.getBean("personService");
		personService.save();

       方法二:使用静态工厂方法实例化

    在方法一的基础上添加一个静态工厂方法,专门实例化bean

  

package com.cvicse.service.impl;
public class PersonServiceBeanFactory {
	public static PersonServiceBean createPersonServiceBean(){
		return new PersonServiceBean();
	}
}

   在applicationContext.xml的XML中配置如下:

<bean id="personService" class="com.cvicse.service.impl.PersonServiceBeanFactory" 
          factory-method="createPersonServiceBean"/>

   在应用类中调用

ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
PersonService personService = (PersonService)cx.getBean("personService");
		personService.save(); 

       方法三:使用实例工厂方法实例化

       在方法一的基础上添加一个工厂方法类,用于bean的实例化

 

package com.cvicse.service.impl;
public class PersonServiceBeanFactory {
	public PersonServiceBean createPersonServiceBean(){
		return new PersonServiceBean();
	}
}

   在applicationContext.xml的XML中配置如下:

<bean id="personServiceFactory" class="com.cvicse.service.impl.PersonServiceBeanFactory"/>
<bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonServiceBean"/>

   在应用类中调用

ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
PersonService personService = (PersonService)cx.getBean("personService");
		personService.save(); 

3、bean的作用域范围

    (1) singleton:默认的情况下在容器启动只有一个bean的实例化对象,在容器实例化的时候初始化。如果添加了lazy-init=“true”则会延迟加载。

    (2) prototype:每次调用都会初始化一个新的实例对象

 <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"/>

    (3) request

    (4) session

    (5)global session

 4、spring容器管理bean的生命周期

      bean默认的情况下是在sping容器实例化的时候被创建,在spring容器关闭之前被销毁。

      在配置文件中可以为每个bean指定初始化方法和销毁方法

 <bean id="personService"
          	class="cn.com.impl.PersonServiceBean"
          	init-method="initMethod" destroy-method="destroyMethod"></bean>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值