spring学习第一天IOC

25 篇文章 0 订阅

目录

spring环境搭建

bean的两种创建规则:applicationcontext  和BeanFactory

bean的三种实例化方式:


 

spring环境搭建

spring各版本下载地址:https://repo.spring.io/release/org/springframework/spring/

创建项目,导入jar包

创建xml文件:

xml头文件从解压包spring-framework-4.2.4.RELEASE-dist/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html复制.

bean的两种创建规则:applicationcontext  和BeanFactory

applicationcontext属于立即加载,在加载配置文件的时候就创建了各个对象

BeanFactory,属于延时加载,是在获取对象的时候创建。

获取的方法:

package cn.pro.Ui;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import cn.pro.dao.ICustormerDao;
import cn.pro.service.CustormerService;

public class Client {

	/**spring的入门案例 
	 * @param args
	 * 
	 * ClassPathXmlApplicationContext   可加载类路径下的配置文件
	 * FileSystemXmlApplicationContext   能够加载磁盘上的配置文件,范围比较大,但是不建议使用  
	 */
	/*public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.获取容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
		
		//2.根据容器id获取对象
		CustormerService cs = (CustormerService) ac.getBean("custormerService");
		
		ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
		cs.saveCustormer();
		
		
		
	}*/
		@SuppressWarnings("all")
		public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.获取容器
			Resource resource = new ClassPathResource("bean.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		
		//2.根据容器id获取对象
		CustormerService cs = (CustormerService) factory.getBean("custormerService");
		
		//ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
		cs.saveCustormer();
		
	
	
}

}

bean的三种实例化方式:

     * 第一种方式:调用默认无参构造函数创建            此种方式应用的最多!!
     *         默认情况下,如果类中没有默认无参构造函数,则创建失败。
     * 
     * 第二种方式:使用静态工厂中的方法创建对象:
     *         需要使用bean标签的factory-method属性,指定工厂中创建对象的方法。
     * 
     * 第三种方式:使用实例工厂中的方法创建

使用静态工厂中的方法创建对象小案例

cn.pro.factory.StaticFactory

package cn.pro.factory;

import cn.pro.service.CustormerService;
import cn.pro.service.impl.CustormerServiceimpl;


//模拟一个静态工厂  
public class StaticFactory {
	public static CustormerService getCustormerService(){
		return new CustormerServiceimpl();
	}
}

xml文件中配置:

<!-- 配置使用静态工厂创建bean对象  -->
<bean id="staticcustormerservice" class="cn.pro.factory.StaticFactory" factory-method="getCustormerService"></bean>

cn.pro.Ui.Client

package cn.pro.Ui;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import cn.pro.dao.ICustormerDao;
import cn.pro.service.CustormerService;

public class Client {

	/**spring的入门案例 
	 * @param args
	 * 
	 * ClassPathXmlApplicationContext   可加载类路径下的配置文件
	 * FileSystemXmlApplicationContext   能够加载磁盘上的配置文件,范围比较大,但是不建议使用  
	 * 
	 * 
	 * Bean的三种创建方式
	 * 第一种方式:调用默认无参构造函数创建            此种方式应用的最多!!
	 * 		默认情况下,如果类中没有默认无参构造函数,则创建失败。
	 * 
	 * 第二种方式:使用静态工厂中的方法创建对象:
	 * 		需要使用bean标签的factory-method属性,指定工厂中创建对象的方法。
	 * 
	 * 第三种方式:使用实例工厂中的方法创建
	 */
		@SuppressWarnings("all")
		public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.获取容器
		Resource resource = new ClassPathResource("bean.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		
		//2.根据容器id获取对象
		CustormerService cs = (CustormerService) factory.getBean("staticcustormerservice");
		//CustormerService cs = (CustormerService) factory.getBean("Instancecustormerservice");
		//ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
		cs.saveCustormer();
		
	
	
}

}

使用实例工厂中的方法创建

cn.pro.factory.InstanceFactory

package cn.pro.factory;

import cn.pro.service.CustormerService;
import cn.pro.service.impl.CustormerServiceimpl;


//模拟一个实例工厂  
public class InstanceFactory {
	public CustormerService getCustormerService(){
		return new CustormerServiceimpl();
	}
}

bean|.xml

<!-- 使用实例工厂创建bean对象 -->
<bean id="instanceFactory" class="cn.pro.factory.InstanceFactory"></bean>
<bean id="Instancecustormerservice" factory-bean="instanceFactory" factory-method="getCustormerService"></bean>

获取bean

Resource resource = new ClassPathResource("bean.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		
		//2.根据容器id获取对象
		CustormerService cs = (CustormerService) factory.getBean("Instancecustormerservice");
		//ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
		cs.saveCustormer();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值