一.IOC和DI的概述
(1)IOC(Inversion of Control)其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源。作为回应,容器适时的返回资源,而应用了IOC以后,则是容器主动地将资源推送给它所管理的组件,组件所需要做的仅是选择一种合适的方式来接受资源,这种行为也被成为查找的被动形式。
(2)DI(Dependency Injection)---IOC的另一种表达方式,即组件以一些预先定义好的方式(例如:setter方法)接受来自:如容器的资源注入。相对于IOC而言,这种表述更直接。
二.配置Bean
(1)配置形式:1.基于xml文件的方式
2.基于注解的方式(以后再解释)
在xml文件中通过bean节点来配置bean:
<!-- 配置bean
class:bean 的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器
id:标识容器中的Bean.id 唯一
-->
<bean id="h" class="com.example.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>
</bean>
从IOC中获取Bean实例的时候,就通过它的唯一标识id来获取,若id没有指定,Spring自动将权限定性类名作为Bean的名字,id可以指定多个名字,名字之间可用逗号,分号,或空格分隔
HelloWorld helloWorld=(HelloWorld)ctx.getBean("h");
System.out.println(helloWorld);
三.Spring容器
(1)在Spring IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化,只有在容器实例化后,才可以从IOC容器里获取Bean实例并使用
(2)Spring提供了两种类型的IOC容器实现
1.BeanFactory:IOC容器的基本实现
2.ApplicationContext提供了更多的高级特性,是BeanFactory的子接口
BeanFactory是Spring框架的基础设施,面向Spring本身,ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext而非底层的BeanFactory,无论使用何种方式,配置文件是相同的。
(3)ApplicationContext
ApplicationContext的主要实现类:1.ClassPathXmlApplicationContext:从类路径下加载配置文件
2.FileSystemXmlApplicationContext:从文件系统中加载配置文件
ConfigurableApplicationContext扩展与ApplicationContext,新增了两个主要的方法:refresh()和close(),让ApplicationContext具有启动,刷新和关闭上下文的能力
ApplicationContext在初始化上下文时就实例化所有单例的Bean
WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作
//ApplicationContext 代表IOC容器(是个接口)
//ClassPathXmlApplicationContext:ApplicationContext的子接口,从类路径下加载配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
(4)从容器中获取Bean的两种方式
//从容器中获取Bean
//利用Id定位到IOC容器中的Bean
HelloWorld helloWorld=(HelloWorld)ctx.getBean("h");
System.out.println(helloWorld);
//利用类型返回IOC容器中的Bean,但是要求IOC容器中必须只能有一个该类型的Bean
HelloWorld helloWorld2=ctx.getBean(HelloWorld.class);
getBean()方法是BeanFactory的方法,由于ApplicationContext是BeanFactory的子类,所以能直接调用,按住Ctrl键,用鼠标点击ApplicationContext,查看源代码,找
ApplicationContext的父类,把BeanFactory粘到show type窗口里,查看类型,就能看到BeanFactory的所有方法(调用eclipse open type窗口的快捷键:Ctrl+shift+t)
四.依赖注入
Spring支持3种依赖注入的方式:
(1)属性注入
(2)构造注入
(3)工厂方式注入(使用很少,不推荐)
1.属性注入
属性注入即通过setter方法注入Bean的属性值或依赖的对象,属性注入使用<property>元素,使用name属性指定Bean的属性名称,value属性或<value>子节点指定属性值,属性注入是实际应用中最常用的注入方式
<!-- 配置bean
class:bean 的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器
id:标识容器中的Bean.id唯一
-->
<bean id="h" class="com.example.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>
</bean>
2.构造方法注入
通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean实例在实例化后就可以使用,构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>里面没有name属性
<!-- 通过构造方法来配置bean的属性 -->
<bean id="car" class="com.example.spring.beans.Car">
<constructor-arg value="Aodi" index="0"></constructor-arg>
<constructor-arg value="shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" index="2"></constructor-arg>
</bean>
<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器 -->
<bean id="car1" class="com.example.spring.beans.Car">
<constructor-arg value="baoma" index="0" type="String"></constructor-arg>
<constructor-arg value="shanghai" index="1" type="String"></constructor-arg>
<constructor-arg value="3000" index="2" type="double"></constructor-arg>
</bean>