第三章 第一个Spring IOC案例(基于xml的配置)

导入必要的jar包

在这里插入图片描述

在类的根路径下创建一个任意名称的xml文件,不能中文

xml资源文件

在xml中导入schema约束

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
      					 http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

把资源交给spring来管理,在配置文件中配置service和dao

<bean id="customerDao" class="com.fuju.study.dao.impl.CustomDaoImpl"></bean>
<bean id="customService" class="com.fuju.study.service.impl.CustomServiceImpl"></bean>

测试是否配置成功

	@Test
   public void testStudy() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		CustomDao customDao=(CustomDao) ac.getBean("customerDao");
		CustomService customService=(CustomService) ac.getBean("customService");
		System.out.println(customDao);
		System.out.println(customService);
   }

IOC细节

  • Spring中工厂的类结构图
    工厂类结构图
  • BeanFactory和ApplicationContext的区别
    • BeanFactory才是Spring容器中的顶层接口。
    • ApplicationContext是它的子接口。
    • BeanFactory和ApplicationContext的区别:
    • 创建对象的时间点不一样。
      • ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
      • BeanFactory:什么时候使用什么时候创建对象。

IOC中bean标签和管理对象细节(重点)

  • bean标签

  • 作用

    • 用于配置对象让spring来创建的。
    • 默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。
  • id:给对象在容器中提供一个唯一标识。用于获取对象。

  • class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。

  • scope:指定对象的作用范围。

    • singleton :默认值,单例的.
    • prototype :多例的.
    • request :WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中.
    • session :WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中.
    • globalSession :WEB项目中,应用在Portlet环境.如果没有Portlet环境那么globalSession相当于session.
  • init-method:指定类中的初始化方法名称。

  • destroy-method:指定类中销毁方法名称。

bean的作用范围和生命周期

  • 单例对象:scope="singleton"
    • 一个应用只有一个对象的实例。它的作用范围就是整个引用。
    • 生命周期:
      • 对象出生:当应用加载,创建容器时,对象就被创建了。
      • 对象活着:只要容器在,对象一直活着。
      • 对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
  • 多例对象:scope="prototype"
    • 每次访问对象时,都会重新创建对象实例。
    • 生命周期:
      • 对象出生:当使用对象时,创建新的对象实例。
      • 对象活着:只要对象在使用中,就一直活着。
      • 对象死亡:当对象长时间不用时,被java的垃圾回收器回收了。

实例化bean的三种方式

  • 使用默认的无参构造方法
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl"/>
  • spring管理静态工厂-使用静态工厂的方法创建对象
public class StaticFactory {	
	public static CustomerService createCustomerService(){
		return new CustomerServiceImpl();
	}
}
<bean id="customerService" 
	  class="com.fuju.factory.StaticFactory" 
	  factory-method="createCustomerService"></bean>
  • spring管理实例工厂-使用实例工厂的方法创建对象
/**
 * 模拟一个实例工厂,创建业务层实现类
 * 此工厂创建对象,必须现有工厂实例对象,再调用方法
 */
public class InstanceFactory {	
	public ICustomerService createCustomerService(){
		return new CustomerServiceImpl();
	}
}
<!-- 此种方式是:
		 先把工厂的创建交给spring来管理。
		然后在使用工厂的bean来调用里面的方法
		factory-bean属性:用于指定实例工厂bean的id。
		factory-method属性:用于指定实例工厂中创建对象的方法。
	-->
	<bean id="instancFactory" class="com.itheima.factory.InstanceFactory"></bean>
	<bean id="customerService" 
		  factory-bean="instancFactory" 
		  factory-method="createCustomerService"></bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值