xml配置文件的基本元素
<beans><!--根元素--> <description> <!-- 零个至一个--> <import> <alias> <bean><!-- 以上为零个到多个--> </beans>使用setter、构造器注入依赖,:
<bean id="calculator" class="org.qbrightwork.studysource.springstudyIoc.Calculator"> <property name="mathUtil" ref="mathUtil"></property> <!--setter--> <constructor-arg ref="mathUtil"></constructor-arg><!--constructor--> <constructor-arg type="String" value="nimei"></constructor-arg> </bean> <bean id="mathUtil" class="org.qbrightwork.studysource.springstudyIoc.MathUtil"></bean>注入集合类List,Set,Map
<bean id="calculator" class="org.qbrightwork.studysource.springstudyIoc.Calculator"> <!-- <property name="mathUtil" ref="mathUtil"></property> --> <!-- <constructor-arg ref="mathUtil"></constructor-arg> --> <constructor-arg type="List" name="list"> <list> <value>list-arg-one</value> <value>list-arg-two</value> <value>list-arg-three</value> </list> </constructor-arg> <constructor-arg ref="mathUtil" /> </bean> <bean id="mathUtil" class="org.qbrightwork.studysource.springstudyIoc.MathUtil"></bean> <bean id="calculator" class="org.qbrightwork.studysource.springstudyIoc.Calculator"> <!-- <property name="mathUtil" ref="mathUtil"></property> --> <!-- <constructor-arg ref="mathUtil"></constructor-arg> --> <constructor-arg type="Set" name="set"> <set> <value>set-arg-one</value> <value>set-arg-two</value> <value>set-arg-three</value> </set> </constructor-arg> <constructor-arg ref="mathUtil" /> </bean> <bean id="mathUtil" class="org.qbrightwork.studysource.springstudyIoc.MathUtil"></bean> <bean id="calculator" class="org.qbrightwork.studysource.springstudyIoc.Calculator"> <!-- <property name="mathUtil" ref="mathUtil"></property> --> <!-- <constructor-arg ref="mathUtil"></constructor-arg> --> <constructor-arg type="Map" name="map"> <map> <entry key='key-one'> <value>value-one</value> </entry> <entry key='key-two'> <value>value-two</value> </entry> <entry key="key-three"> <value>value-three</value> </entry> </map> </constructor-arg> <constructor-arg ref="mathUtil" /> </bean> <bean id="mathUtil" class="org.qbrightwork.studysource.springstudyIoc.MathUtil"></bean>如果要向一个属性注入null值是,应该使用标签<null /> 而不是 <value></value>
autowire属性:让IoC容器自己识别需要注入那些类到bean中去。可以选择的参数有:byType、byName、no、default、constructor五个属性,其中比较常用的是byName属性,就是根据类中的属性名称进行注入。
parent属性,当类存在继承关系时,可以使用parent属性指定其父类
bean的scope属性:
singleton:在Bean容器中只有一个实例,说有对象共享一个实例(默认配置)prototype:容器在接到该类型对象的请求的hih会每次都重新生成一个新的对象实例给请求方,当将实例交给请求方之后,便不再管理实例的生命周期。request:用在web情境中,向每个请求返回全新的实例,可以看做是prototype的一种特例session:用在web情景中,比scope为request的bean具有更长的生命周期global session :用于protlet的web程序6自定义scpoe,需要实现scope接口转载于:https://www.cnblogs.com/Qbright/archive/2012/07/17/2592284.html