<!--加载prpperties配置文件信息-->
<!--将user对象交给spring管理-->
<!--创建对象的三种方式:
1.构造器:无参构器,有参构造器,
2.静态工厂造
3.实例工厂
-->
<!--1.构造器--> <bean name="book" class="com.ujy.pojo.Book"> <constructor-arg name="bid" value="1"/> <constructor-arg name="bookName" value="三国演义"/> <constructor-arg name="price" value="99"/> <constructor-arg name="author" value="罗贯中"/> </bean> <!--2.静态工厂:通过类的静态方法的返回值创建对象--> <bean id="book03" class="com.ujy.pojo.StaticFactory" factory-method="getBook"></bean> <!--3.实例工厂:通过类对象的普通方法返回值得到的对象--> <bean id="instanceFactory" class="com.ujy.pojo.InstanceFactory" ></bean> <bean id="book04" factory-bean="instanceFactory" factory-method="getBook"></bean>
二、 给对象的属性赋值的三种方式:
1:按照值得类型划分
值注入 :基本类型
引用注入 :引用其他类型
<!--值注入 value--> <bean id="book011" class="com.ujy.pojo.Book"> <property name="bid" value="10"></property> </bean> <!--引用注入 ref--> <bean class="com.ujy.pojo.Person"> <property name="name" value="wangwu"/> <property name="book" ref="book011"/> </bean>
2.一:按照属性注入方式划分
1.set方法
property
<bean id="book11" class="com.ujy.pojo.Book">
<property name="bid" value="10"/>
<property name="bookName" value="童年的故事"/>
<property name="price" value="200"/>
<property name="author" value="zhangsan"/>
</bean>
p名称空间
<!--p命名空间-->
<bean id="bo1" class="com.ujy.pojo.Book" p:bid="12" p:author="zhang" p:price="33"></bean>
2.构造器
通过有参构造器 还有index 默认从0开始 多个构造器的执行顺序 执行哪一个
type:指定参数的类型
任何一个构造器 都可以指定该构造器的name,index,type 来确定
某个特定的构造器。
还有 就是构造器 就是随着初始化就被创建 。
<bean id="bo2" class="com.ujy.pojo.Book">
<constructor-arg name="bid" value="104" type="java.lang.Integer"/>
<constructor-arg name="bookName" value="金色的海洋"/>
<constructor-arg name="author" value="津津"/>
<constructor-arg name="price" value="100" index="1"/>
</bean>
3.注解
三、给对象的复杂属性赋值
1.数组 如果是单值,用value 或者ref
如果是多值,用array标签内部用value,bean,ref
2.列表,如果是单值,用value或者ref
多值用list标签,内部用value,bean,ref
3.set:如果是单值,用value,bean,ref
多值用set标签,内部用value,bean,ref
4,properties ,用props标签 ,里面嵌套prop标签
底层使用的map ,键和值都是String类型
<bean class="com.ujy.pojo.ComplexBean" > <property name="arr"> <array> <value>12</value> <value>张三</value> <bean class="com.ujy.pojo.Book"></bean> </array> </property>
<property name="list"> <list> <value>1234</value> <ref bean="book011"></ref> </list> </property>
<!-- <property name="list" ref="bo2"/>--> <property name="map" > <map> <entry key="key1" value="value1"></entry> <entry key="key2" value-ref="book011"></entry> <entry key-ref="book011" value="value3"></entry> <entry key-ref="book011" value-ref="book011"></entry> </map> </property>
<property name="properties"> <props> <prop key="key1">value1</prop> <prop key="ddd">value2</prop> </props> </property> </bean>
下面是这几种复杂属性的测试方法
//复杂属性注入值 @Test public void test7() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ComplexBean bean = applicationContext.getBean(ComplexBean.class); Object[] arr = bean.getArr(); for (Object obj : arr) { System.out.println(obj); } } @Test public void test8() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ComplexBean bean = applicationContext.getBean(ComplexBean.class); List list = bean.getList(); for (Object object : list) { System.out.println(object); } } @Test public void test9() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ComplexBean bean = applicationContext.getBean(ComplexBean.class); // Map map = bean.getMap(); // Set set = map.entrySet(); // for (Object s : set) { // System.out.println(s); // } Map map = bean.getMap(); Set<Map.Entry> set = map.entrySet(); for (Map.Entry entry : set) { System.out.println(entry.getKey() + "::" + entry.getValue()); } } @Test public void test10() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ComplexBean bean = applicationContext.getBean(ComplexBean.class); Properties properties = bean.getProperties(); Set<Map.Entry<Object, Object>> entries = properties.entrySet(); for (Map.Entry entry:entries){ System.out.println(entry.getKey()+"::"+entry.getValue()); } }