IOC学习实验1-4

13 篇文章 0 订阅

ioc容器注册

注册一个person对象,spring会自动创建这个person对象
一个bean标签可以注册一个组件
class:写要注册的组件的全类名
id:这个对象的唯一标识
使用property为person对象的属性赋值
name=“lastName” 指定属性名 value=“张三” 属性值

<bean id="person01" class="com.yj.bean.person">
	<property name="lastName" value="张三"></property>
	<property name="age" value="20"></property>
	<property name="email" value="qq.com"></property>
	<property name="gender" value="f"></property>
	</bean>

调用

ApplicationContest
* 1)new ClassPathXmlApplicationContext(“ioc.xml”) ioc容器在类路径下
* new FileSystemXmlApplicationContext(“F://ioc.xml”) ioc容器在磁盘路径下
* 2)给容器中注册一个组件,也从容器中按照id拿取
* 3)容器中的对象实在容器创建一完成就也跟着被创建了
* 4)同一个组件在ioc容器中是单实例的,永远只操作这一个,容器启动之前就已经创建完成
* 5)容器中如果没有这个组件会报异常
* 6)ioc容器在创建这个组件对象时,会利用setter方法为JavaBean的属性进行赋值
* 7)javaBean的属性名是由getter和setter方法决定的

 	@Test
	public void test() {
		//ApplicationContext代表ioc容器
		//当前应用的xml配置文件在ClassPath下
		//根据spring的配置文件得到ioc容器对象
		ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
		
		//容器帮我创建好了对象
		person bean = (person) ioc.getBean("person01");
		System.out.println(bean);
	}

实验2:根据bean的类型从IOC容器中获取bean的实例★

 * 如果ioc容器中这个类型的bean有多个,查找就会报错
 * org.springframework.beans.factory.NoUniqueBeanDefinitionException:
 *  No qualifying bean of type [com.yj.bean.person] is defined: 
 *  expected single matching bean but found 2: person01,person02
	 
person bean2 = ioc.getBean("person02", person.class);
		System.out.println(bean2);

实验3: 通过构造器为bean的属性赋值(index,type属性介绍) 通过p名称空间为bean赋值

	<bean id="person03" class="com.yj.bean.person">
	<!-- 调用有参构造器进行创建对象和赋值,要求掌握 -->
	<!-- public person(String lastName, Integer age, String gender, String email) { -->
	<constructor-arg name="lastName" value="小米"></constructor-arg>
	<constructor-arg name="gender" value="男"></constructor-arg>
	<constructor-arg name="email" value="wu"></constructor-arg>
	<constructor-arg name="age" value="21"></constructor-arg>
	</bean>
	<!-- 省略name属性,但要严格遵守有参的位置 -->
	
	<bean id="person04" class="com.yj.bean.person">
	<constructor-arg value="小哈"></constructor-arg>
	<constructor-arg value="14" type="java.lang.Integer"></constructor-arg>
	<constructor-arg value="女"></constructor-arg>
	<constructor-arg value="163.com" index="3"></constructor-arg>
	<!-- index为参数指定索引,从0开始 -->
	<!-- 但若是有两个构造器的参数个数一样就会出问题就要使用type明确的确定value的类型 -->
	</bean>
	<!-- 通过p名称空间为bean赋值,要导入p名称空间 -->
	<!-- 名称空间:防止标签重复 -->
	
	<bean id="person05" class="com.yj.bean.person"
	p:age="100" p:email="guigu.com" p:lastName="hahha" p:gender="男">
	</bean>

实验4:正确的为各种属性赋值

测试使用null值 
引用类型赋值(引用其他bean、引用内部bean)
	 <bean id="car01" class="com.yj.bean.car" 
     p:carName="BMW" p:color="green" p:price="3000">
     </bean>
  <bean id="person01" class="com.yj.bean.person">
	<property name="lastName">
	<!-- 进行复杂的赋值 -->
	<null></null>
	</property>
	<!-- ref引用      引用外部bean-->
	<!-- <property name="ca" ref="car01"-->
	<!-- 引用类型赋值(引用其他bean、引用内部bean) -->
	<!--</property> -->
	<property name="ca">
	<!-- 相当于new了一个car  引用内部bean -->
	<bean class="com.yj.bean.car">
		<property name="carName" value="自行车"></property>
	</bean>
	</property>
</bean>
集合类型赋值(List、Map、Properties)
  <bean id="book01" class="com.yj.bean.Book">
  <property name="bookName" value="东游记"></property>
  </bean>
    <bean id="person02" class="com.yj.bean.person">
	<!-- 如何为list赋值 -->
	<property name="books">
<!-- 相当于books = new arraylist<book>() -->
	<list>
		<bean id="book0" class="com.yj.bean.Book" p:bookName="西游记"></bean>
		<ref bean="book01"/>
	</list>
</property>
	<!-- 如何为map赋值 -->
	<property name="maps">
	<!-- 相当于maps = new hashMap<>() -->
<map>
	<!-- 一个entry代表一个键值对 -->
	<entry key="key01" value="张三"></entry>
	<entry key="key02" value="15"></entry>
	<entry key="key03" value-ref="book01"></entry>
	<entry key="key04" >
		<bean class="com.yj.bean.car">
			<property name="carName" value="map中内部生成的car"></property>
		</bean>
	</entry>
	<!-- <entry key="key05">
	<map></map>
	</entry> -->
</map>

</property>

<property name="properties">
<!-- 所有类型都是String -->
	<props>
		<prop key="userName"> root</prop>
		<prop key="password">123456</prop>
	</props>
</property>

</bean>

util名称空间创建集合类型的bean


</bean>
<bean id="person03" class="com.yj.bean.person">
<property name="maps" ref="mymap"></property>
</bean>
 <util:map id="mymap">
<!-- 只是在其中添加key&value -->
<entry key="key01" value="张三"></entry>
	<entry key="key02" value="15"></entry>
	<entry key="key03" value-ref="book01"></entry>
	<entry key="key04" >
		<bean class="com.yj.bean.car">
			<property name="carName" value="map中内部生成的car"></property>
		</bean>
	</entry>
<!-- <entry key="key05">
<map></map>
</entry> -->
</util:map>

级联属性赋值,可以修改属性的属性

<bean id="person04" class="com.yj.bean.person">
<!-- 为car赋值的时候,改变car的价格 -->
<property name="ca" ref="car01"></property>
<property name="ca.price" value="1"></property>
</bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值