在SpringIOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化,只有在容器实例化后,才可以从IOC容器中获取Bean实例并使用。
Spring提供了两种类型的IOC容器实现:
-BeanFactory:IOC容器的基本实现
-ApplicationContext:提供了更多的高级特性,是BeanFactory的子接口。
-BeanFactory是Spring框架的基础设施,面向Spring本身:
ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContexterer而非底层的BeanFactory
-无论何种用方式,配置文件是相同的
ApplicationContext(代表IOC容器):
ApplicationContext的主要实现类:
-ClassPathXmlApplicationContext:从类路径下加载配置文件
-FileSystemXmlApplicationContext:从文件系统中加载配置文件
ConfigurableApplicationContext扩展于ApplicationContext,新增的两个主要方法:refresh()和close(),让ApplicationContext具有启动、刷新和关闭上下文的能力
ApplicationContex在初始化上下文时就实例化所有单例的Bean
WebApplacationContext是专门为WEB应用而准备的,它允许熊相对于WEB根目录的路径中完成初始化工作
ApplicationContext(interface) | |
ConfigurableApplicationContext | |
ClassPathXmlApplicationContext | FileSystemXmlApplicationContext |
从IOC容器中获取Bean:调用ApplicationContext的getBean()方法
依赖注入的方式(3种):
-属性注入:通过setter方法注入Bean属性值或依赖的对象,使用<property>元素,使用name属性指定Bean的属性名称,value属性或<value>子节点指定的属性值,是最常用的注入方式
-构造器注入:通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean实例在实例化后就可以使用,构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>中没有name属性
注:使用构造器注入属性值可以指定参数的位置和参数的类型,以区分重载的构造器:
eg:
<bean id="car" class="com.at.spring.beans.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来:eg:让value为<上海>
<constructor-arg type="java.lang.String">
<value><![CDATA[<Shanghai>]]></value>(属性值也可以使用value子节点进行配置)
</constructor-arg>
<constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
<constructor-arg value="2000" index="4"></constructor-arg>
</bean>
使用property的ref属性建立bean之间的引用关系
比如现在增加一个Person类,有name和age属性
<bean id="person" class="com.at.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="18"></property>
<!--使用property的ref属性建立bean之间的引用关系 -->
<property name="car" value="car"></property>
</bean>
内部bean(不能被外部bean引用,只能在内部使用):
<bean id="person" class="com.at.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="18"></property>
<property name="car" >
<bean class="com.at.spring.beans.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
<constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>
</property>
</bean>
-工厂方法注入(很少使用,不推荐)
注入参数详解:
null值和级联属性:
可以使用专用的<null/>元素标签为Bean的字符串或其他对象类型的属性注入null值(实际上并没有什么意义)
<constructor-arg><null/></constructor-arg>
和Struts、Hiberante等框架一样,Spring支持级联属性的配置。
级联属性(缺乏了解):https://blog.csdn.net/aigo6/article/details/48830433(网摘)注意属性需要先初始化后才可以为级联属性赋值,这一点和Struts不同
集合属性的注入:
<bean id="person" class="com.at.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="18"></property>
<property name="car" >
<list>(还有map/set等等)
<!--内容-->
</list>
</property>
</bean>
使用utllity scheme定义集合 (这样的bean可以被共享),相对于传统的配置方式更加简洁:
可以使用utllity scheme里的集合标签定义独立的集合Bean,需要注意的是,必须在<beans>根元素里添加utllity scheme定义
<util:list id="cars"></util:list>
使用p命名空间(通过简化的方式配置bean的属性),需要先导入p命名空间:
<bean id="person" class="com.at.spring.beans.Person" p:age="30" p:name="Tom" p:cars-ref="cars"></bean>