首页 > 学院 > 开发设计 > 正文

spring ioc容器的注入

2019-11-06 08:39:42
字体:
来源:转载
供稿:网友

属性的注入就是通过配置bean

eg:

一、普通方法配置bean

<bean id="helloWord" class="com.sPRing.test.Helloword">        <property name="name" value="spring"></property>    </bean><!--配置bean class:bean的全类名,通过反射的方式在ioc容器中创建bean。所以要求bean中必须有无参数 构造器。 id:  标示bean的名称,在ioc中是必须是唯一的。若iod没有指定,Spring自动将权限定性类名作为Bean的名字 id可以指定多个名字,名字之间可以用","”:“或空格隔开, -->

二、通过构造方法配置bean

<!-- 通过构造方法配置bean属性 -->	<bean id="car" class="com.spring.test.Car">		<constructor-arg value="Audi" index="0"></constructor-arg>		<constructor-arg index="1">			<!-- 如果字面包含特殊字符可以使用如下方式进行包裹 <![CDATA[]]> -->			<!-- 属性值使用value子节点进行配置 -->			<value><![CDATA[《上海》]]></value>		</constructor-arg>		<constructor-arg value="30000" type="double"></constructor-arg>	</bean>说明:这个bean就是通过构造方法配置bean属性,和上边的配置的方式效果相同,主要区别就是上边的是通过<property>标签进行配置  这里是通过<construcor-arg>

进行配置。这里的construct-arg标签相当于<property>  其中value的属性值就是为属性所赋的值,index属性是根据类的属性顺序进行排序,  也可以通过type属性来限定

属性赋值。这样可以更精确,避免了多个类型的赋值发生冲突。下边就是一个具体的例子:如下

<!-- 使用构造器注入属性可以指定参数的位置和参数类型,以区分重载的构造器 -->	<bean id="car2" class="com.spring.test.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>

三、引入外部bean

例子如下

<bean id="person" class="com.spring.test.Person">		<property name="name" value="TOM"></property>		<property name="age" value="24"></property>		<!-- 引用其他Bean 组成应用程序的Bean经常需要相互协作以完成应用程序的功能,要使Bean能够相互访问, 就必须在Bean配置文件中指定队Bean的应用 			在Bean配置我呢就爱那种可以通过<ref>元素或ref属性为bean的属性或构造器参数指定队Bean的应用。 也可以在输习惯或构造器里包含Bean的声明,这样的Bean成为内部Bean。 			<ref bean="car2"> <property name="car" ref="car2"></property> -->		<property name="car">		<ref bean="car2" /> 		</property>	</bean>说明:上边的例子是嵌入内部bean。下边是一个建立一个内部bean四、建立一个内部bean
<bean id="person" class="com.spring.test.Person">		<property name="name" value="TOM"></property>		<property name="age" value="24"></property>		<!-- 引用其他Bean 组成应用程序的Bean经常需要相互协作以完成应用程序的功能,要使Bean能够相互访问, 就必须在Bean配置文件中指定队Bean的应用 			在Bean配置我呢就爱那种可以通过<ref>元素或ref属性为bean的属性或构造器参数指定队Bean的应用。 也可以在输习惯或构造器里包含Bean的声明,这样的Bean成为内部Bean。 			<ref bean="car2"> <property name="car" ref="car2"></property> -->		<property name="car">			<bean name="car3" class="com.spring.test.Car">			<constructor-arg value="Ford"></constructor-arg>			<constructor-arg value="Changan"></constructor-arg>			<constructor-arg value="2000"></constructor-arg>		</bean>			</property>	</bean>五、级联属性赋值
	<bean id="person2" class="com.spring.test.Person">		<constructor-arg value="Jerry"></constructor-arg>		<constructor-arg value="25"></constructor-arg>		<constructor-arg ref="car"></constructor-arg>	<!-- 为级联属性赋值 -->		<property name="car.price" value="300000"></property>		<property name="car.maxSpeed" value="250"></property>	</bean>

六、 测试如何配置集合属性 如果存在这样一个属性

private   List<car> car;

我们可以采取如下的配置集合的属性来配置bean属性。

	<bean id="person3" class="com.atguigu.spring.test.collection.Person">	<property name="name" value="Mike"></property>		<property name="age" value="27"></property>		<property name="car">			<!-- 为list集合赋值。 -->			<list>				<ref bean="car"/>				<ref bean="car2"/>			</list>		</property>七、如何配置Map  bean属性

如果存在一个这样的属性(private  Map<String,Car>),我们可以采取Map的配置bean属性的方法。

<bean id="newPerson" class="com.atguigu.spring.test.collection.NewPerson">		<property name="name" value="Rose"></property>		<property name="age" value="28"></property>		<property name="car">			<!-- 使用map节点的entry子节点配置Map类型的成员变量java.util.Map通过<map>标签	定义,<Map>标签里可以使用多个<entry>作为子标签,每个条目包含一个件和一个值。			必须在<key>标签里定义键,因为键和值的类型没哟限制,所以可以自由地为他们指定<value>,<ref><bean>或<null>yu元素。	可以将Map的键和值作为<entry>属性定义,简单常量使用key和value来定义,Bean引用通过key-ref和value-ref属性定义	使用<props>定义java.util.Properties.该标签使用多个<prop>作为字标签,每个<prop>标签定义key属性。			-->			<map>				<entry key="AA" value-ref="car"/>				<entry key="BB" value-ref="car2"/>			</map>		</property>	</bean>七、配置properties属性值
	<!-- 配置properties屬性值 -->		<bean id="propertires" class="com.atguigu.spring.test.collection.DataSource">			<property name="properties">			<!-- 使用<props>定义java.util.Properties.该标签使用多个<prop>作为字标签,每个<prop>标签定义key属性。	 -->			<props>			<prop key="user">root</prop>				<prop key="password">wdl03707552882</prop>				<prop key="jdbcUrl">jdbc:MySQL://test</prop>				<prop key="driverClass">com.mysql.jdbc.Driver</prop>			</props>		</property></bean>说明:使用该方法配置bean是在类中存在如下属性  private  Properties   properties;这是java.util的系统的属性,他是配置键值对的常用方式。八、配置单利的bean的。以供bean进行引用,需要导入util命名空间
	<!-- 配置单利的bean,以供bean进行引用,需要导入util的命名空间。 -->	<util:list id="cars">		<ref bean="car"/>		<ref bean="car2"/>	</util:list>	<bean id="person4" class="com.atguigu.spring.test.collection.Person">		<property name="name" value="Jack"></property>		<property name="age" value="29"></property>		<property name="car" ref="cars"></property>	</bean>

九、通过p命名空间对应的属性赋值,需先导入p命名空间,相对于传统的方式更为简洁。
<!-- 通过p命名空间对应的属性赋值 ,需要先导入p命名空间,相对于传统的方式更为简洁-->	<bean id="person5" class="com.atguigu.spring.test.collection.Person" p:age="30"p:name="Queen" p:car-ref="cars">	</bean>十、 可以使用autowire.属性指定自动装配的方式,

	<!-- <bean id="person" class="com.spring.beans.autowires.Person" p:name="Tom" p:address-ref="address" p:car-ref="car"></bean> -->	<bean id="person" class="com.spring.beans.autowires.Person" p:name="Tom" autowire="byName"></bean>

注意:byName根据bean的名字和当前bean的setter和getter风格属性进行自动装配byType根据bean的属性类型和当前bean的属性类型自动装配,若IOC容器中有一个以及以上的类型匹配的bean,则抛出异常。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表