4.SpringIOC实验

SpringIOC实验

  1. 实验2 根据bean的类型从IOC容器中获取bean实例
	/**
	 * 实验2 根据bean的类型从IOC容器中获取bean实例
	 * 如果IOC容器中这个类型的bean有多个,则会出错,只能通过id来查找
	 * 也可以通过id和类型来查找
	 */
	@Test
	public void test2(){
   
		Person bean=ioc.getBean(Person.class);
		System.out.println(bean);
		
		Person bean2=ioc.getBean("person1",Person.class);
		System.out.println(bean2);
	}

  1. 实验3 调用有参构造器
/**
 * 实验3 调用有参构造器
 */
@Test
public void test3(){
   
	Person bean=(Person) ioc.getBean("person03");
	System.out.println(bean);
}
<bean id="person03" class="com.atguigu.bean.Person">
	<!-- 调用有参构造器进行创建对象并复制 -->
	<constructor-arg name="lastName" value="里斯"></constructor-arg>
	<constructor-arg name="email" value="2200123456@qq.com"></constructor-arg>
	<constructor-arg name="gender" value=""></constructor-arg>
	<constructor-arg name="age" value="52"></constructor-arg>
</bean>

通过p名称空间

<!-- 通过P名称空间为bean赋值,导入p名称空间(在下面的namespace在添加)
	名称空间:在xml中名称空间是用来防止标签重复的
	步骤:1.导入p名称空间 2.写前端的标签属性-->
	<bean id="person06" class="com.atguigu.bean.Person"
		p:age="18" p:lastName="小王" p:email="111@qq.com" p:gender="">
	</bean>
  1. 实验4正确地为各种属性赋值
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--实验4正确地为各种属性赋值  -->
<!-- 
    测试使用null值 、
    引用类型赋值(引用其他bean、引用内部bean)
    集合类型赋值(List、Map、Properties)
    util名称空间创建集合类型的bean
    级联属性赋值
 -->
 <bean id="car01" class="com.atguigu.bean.Car">
 	<property name="carName" value="宝马"></property>
 	<property name="color" value="red"></property>
 	<property name="price" value="300000"></property>
 </bean>
 <bean id="person01" class="com.atguigu.bean.Person">
 	<property name="lastName">
 		<null/>
 	</property>
<!--  	<property name="car" ref="car01"></property> -->
	<property name="car">
		<!-- 对象我们可以使用bean标签创建car=new Car(),引用内部bean,不能被获取到,只能内部使用-->
		<bean id="car02" class="com.atguigu.bean.Car">
			<property name="carName" value="本茨"></property>
		</bean>
	</property>
 </bean>
 
 <bean id="book01" class="com.atguigu.bean.Book">
 	<property name="bookName" value="老友记"></property>
 </bean>
 <!-- List<Book> books; -->
 <bean id="person02" class="com.atguigu.bean.Person">
 	<!-- 如何为list类型赋值 -->
 	<property name="books">
 		<!-- books=new ArrayList<Book>(); -->
 		<list>
 			<!-- list标签中添加每一个元素 -->
 			<bean id="book000x" class="com.atguigu.bean.Book" p:bookName="西游记"></bean>
 			<!-- 引用一个外部元素 -->
 			<ref bean="book01"/>
 		</list>
 	</property>
 	<!-- Map<String,Object> maps; -->
 	<property name="maps">
 		<!-- maps=new LinkedHashMap<>(); -->
 		<map>
 			<!-- 一个entry就代表一个键值对 -->
 			<entry key="key01" value="张三"></entry>
 			<entry key="key02" value=
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值