spring-配置Bean2

引用其它 Bean

在 Bean 的配置文件中, 可以通过 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用.
也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

<bean id="person" class="com.spring.beans.Person">
		<property name = "name" value = "dirac"  ></property >
		<property  name = "age" value = "21" ></property >
		<!-- 
		<property name="car" ref="car2"></property>
		 -->
		 <!-- 内部Bean不能被外部引用,只能内部引用 --> 
		 <property name="car">
		 	<bean id ="car3" class = "com.spring.beans.Car">
		 		<constructor-arg value = "Ford" index ="0"></constructor-arg>
				<constructor-arg value = "ChangChen" index = "1"></constructor-arg>
				<constructor-arg value = "10000" type="double"></constructor-arg>	
			</bean>
		 </property> 

集合属性

1.配置 java.util.List 类型的属性, 需要指定 标签, 在标签里包含一些元素. 这些标签可以通过 指定简单的常量值, 通过 指定对其他 Bean 的引用. 通过 指定内置 Bean 定义. 通过 指定空元素. 甚至可以内嵌其他集合.
2.Java.util.Map 通过 标签定义, 标签里可以使用多个 作为子标签. 每个条目包含一个键和一个值.
必须在 标签里定义键
可以将 Map 的键和值作为 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义
使用 定义 java.util.Properties, 该标签使用多个 作为子标签. 每个 标签必须定义 key 属性.

<bean id="person3" class="com.spring.beans.collections.Person">
		<property name = "name" value ="Mike"></property>
		<property name = "age" value ="27"></property>
		<property name = "cars"  >
		<!-- 使用list节点为List类型属性赋值 -->
			<list>
				<ref bean = "car"></ref>
				<ref bean = "car2"></ref>
				<bean class = "com.spring.beans.Car">
		 			<constructor-arg value = "Ford" index ="0"></constructor-arg>
					<constructor-arg value = "ChangChen" index = "1"></constructor-arg>
					<constructor-arg value = "10000" type="double"></constructor-arg>	
				</bean>
			</list>
		
		</property>
	
	</bean>
	<!-- 配置Map属性值 -->
	<bean id="newPerson" class = "com.spring.beans.collections.NewPerson">
	<property name = "name" value ="Rose"></property>
		<property name = "age" value ="27"></property>
		<property name = "cars"  >
		<!-- 使用map节点及map的entry子节点配置Map类型的成员变量 -->
		<map>
			<entry key="AA" value-ref="car"></entry>
			<entry key="BB" value-ref="car2"></entry>
		</map> 
	</property>
	</bean>

配置 Properties,新建DataSource类,最后在Main.java测试

DataSource dataSource = ctx.getBean(DataSource.class); sy

<!-- 配置Properties属性值 -->
	<bean id="dataSource" class = "com.spring.beans.collections.DataSource">
		<property name="properties">
		<!-- 使用props和prop为属性赋值 -->
			<props>
				<prop key = "user">root</prop>
				<prop key = "password">root</prop>
				<prop key = "jdbcUrl">jdbc:mysql://localhost:3306/database</prop>
				<prop key = "driverClass">com.mysql.jdbc.Driver</prop>
			</props>
		</property>
	</bean>

使用 utility scheme 定义Bean集合

	<util:list id = "cars">
		<ref bean= "car"/>
		<ref bean= "car2"/>	
	</util:list>
	<bean id="person4" class = "com.spring.beans.collections.Person">
		<property name = "name" value ="Jack"></property>
		<property name = "age" value ="27"></property>
		<property name = "cars" ref="cars"></property>	 
	</bean>

使用 p 命名空间

<!-- 通过p命名空间为bean的属性赋值,需要先导入p命名空间,相对于传统配置方式更简洁 -->
	<bean id ="person5" class = "com.spring.beans.collections.Person" 
	p:age="30" p:name="Queen" p:cars-ref="cars"></bean>
发布了15 篇原创文章 · 获赞 0 · 访问量 117

猜你喜欢

转载自blog.csdn.net/weixin_38235865/article/details/103745479