bean的配置
一.采用xml(反射)注入bean
<bean id="helloWorld" class="HelloWorld">
</bean>
id是bean的唯一表示
class是bean的全类名,通过反射的方式在ioc容器中创建bean,要求bean中必须有无参构造器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
helloWorld.hello();
Spring提供两种ioc的实现方式
--BeanFactory ioc容器的基本实现
是Spring框架的基础设施,面向Spring本身
--ApplicationContext提供了更多的高级特性,是BeanFactory的子接口
面向开发者,几乎所有的应用场合都直接使用ApplicationContext而非底层的BeanFactory
无论使用何种方式,配置文件时都相同
ApplicationContext的继承结构:
ApplicationContext有两个主要的实现类:
--ClassPathXmlApplicationContext 从类路径下加配置文件
--FileSystemXmlApplicationContext 从文件系统中加载配置文件
两个实现类都是实现了ConfigurableApplicationContext接口
ConfigurableApplicationContext扩展了ApplicationContext,新增加了两个重要的方法refresh()和close(),使ApplicationContext具有启动、刷新和关闭上下文的功能
ApplicationContex在初始化上下文时就实例化所有的单例的bean
WebApplicationContex web专用类
ApplicationContext使用getBean(String str) 方法调用实例
方法由BeanFactory接口定义 ApplicationContext接口继承
二、依赖注入的方式
Spring支持3种依赖注入的方式
--属性注入
<bean id="helloWorld" class="HelloWorld">
<property name="name" value="helloWord"></property>
</bean>
<property name="name" value="helloWord"></property>
</bean>
属性注入即通过setter方法注入Bean的属性值或依赖的对象
属性注入使用<property>元素,使用name属性指定Bean的属性名称,value属性或<value>子节点指定Bean
属性注入是实际应用中最常用的注入方式
--构造器注入
<bean id="car" class="Car">
<constructor-arg value="Audi" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg value="ShangHai" index="1" type="java.lang.String"></constructor-arg>
<constructor-arg value="300000" index="2" type="int"></constructor-arg>
</bean>
<constructor-arg value="Audi" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg value="ShangHai" index="1" type="java.lang.String"></constructor-arg>
<constructor-arg value="300000" index="2" type="int"></constructor-arg>
</bean>
通过构造方法注入Bean的属性值或依赖对象,他保证了Bean实例在实例化后就可以使用
构造器注入在<constructor-arg>元素里声明属性,index属性用来标识参数位置,type属性用来表示参数类型,以区分重载的构造器。<constructor-arg>中没有name属性
<constructor-arg index="1" type="java.lang.String"> <value><![CDATA[<shangHai^>]]></value> </constructor-arg>
特殊字符使用<![CDATA[ ]]>进行注入
--工厂方法注入
二、建立Bean之间的引用关系
--使用ref属性
<bean id="person" class="Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car" ref="car"></property>
</bean>
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car" ref="car"></property>
</bean>
使用ref属性可以引用其他的bean进行注入,建立引用关系
--使用内部bean
<bean id="person" class="Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car">
<bean class="Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="changAn"></constructor-arg>
<constructor-arg value="200000"></constructor-arg>
</bean>
</property>
</bean>
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car">
<bean class="Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="changAn"></constructor-arg>
<constructor-arg value="200000"></constructor-arg>
</bean>
</property>
</bean>
内部bean,不能被外部应用,只能在内部使用
三、进行级联赋值
<bean id="person" class="Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car" ref="car"></property>
<property name="car.maxSpeed" value="250"></property>
</bean>
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car" ref="car"></property>
<property name="car.maxSpeed" value="250"></property>
</bean>
使用属性.属性的方式进行级联赋值,在car为创建的时候无法为其属性赋值(属性需要先初始化后才可以为级联属性赋值,否则会有异常 )
四、配置集合属性
<bean id="collectionPerson" class="collection.Person">
<property name="name" value="Mike"></property>
<property name="age" value="27"></property>
<property name="car">
<list>
<ref bean="collectionCar"></ref>
</list>
</property>
</bean>
<property name="name" value="Mike"></property>
<property name="age" value="27"></property>
<property name="car">
<list>
<ref bean="collectionCar"></ref>
</list>
</property>
</bean>
在Spring中可以通过一组内置的Xml标签(比如:<list>,<set>,<map>)来配置集合属性
配置List类型的属性,需要制定<list>标签,在标签里包含一些元素,这些标签可以通过<value>指定简单的常量值,通过<ref>指定对其他Bean的引用,通过<bean>指定内置Bean定义,甚至可以内嵌其他集合
数组的定义和List一样,都使用<list>
五、配置Map和Properties属性
<bean id="personMap" class="collection.PersonMap">
<property name="name" value="Rose"></property>
<property name="age" value="28"></property>
<property name="car">
<map>
<entry key="baoMa" value-ref="collectionCar"></entry>
</map>
</property>
</bean>
<property name="name" value="Rose"></property>
<property name="age" value="28"></property>
<property name="car">
<map>
<entry key="baoMa" value-ref="collectionCar"></entry>
</map>
</property>
</bean>
Map通过<map>标签定义,<map>标签里可以使用多个<entry>作为子标签,每个条目包含一个键和一个值
<bean id="dataSource" class="collection.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
</props>
</property>
</bean>
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
</props>
</property>
</bean>
使用<props>定义Properties,该标签使用多个<prop>作为子标签,每个<prop>标签必须定义key属性
六、使用utility scheme定义集合
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:list id="cars">
<ref bean="collectionCar"></ref>
</util:list>
<ref bean="collectionCar"></ref>
</util:list>
使用基本的集合标签定义集合时,不能将集合作为独立的Bean定义,导致其他Bean无法引用该集合,所以无法在不同的Bean之间共享集合
必须在<beans>中定义添加util schema定义
使用ref元素调用
七、使用p命名空间简化bean的赋值
xmlns:p="http://www.springframework.org/schema/p"
<bean id="PersonP" class="collection.Person"
p:age="30" p:name="Queen" p:car-ref="collectionCar"></bean>
p:age="30" p:name="Queen" p:car-ref="collectionCar"></bean>
必须在<beans>中添加p定义