使用spring对类对象的简单类型属性和对象属性赋值

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5     <!--简单类型的set注入,简单类型,java基本类型和String类型都是简单类型-->
 6     <!--对象类型的属性赋值,ref=“beanid”-->
 7     <bean id="myStudent" class="com.bjpowernode.ba02.Student">
 8         <property name="age" value="20"/>
 9         <property name="name" value="张三"/>
10         <property name="school" ref="mySchool"/>
11     </bean>
12     <bean id="mySchool" class="com.bjpowernode.ba02.School">
13         <property name="name" value="一中"/>
14         <property name="address" value="莒县"/>
15     </bean>
16 </beans>
 1 package com.bjpowernode;
 2 
 3 import com.bjpowernode.ba02.Student;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 public class MyTest01 {
 9     @Test
10     public void test01(){
11         String path = "ba02/applicationContext.xml";
12         ApplicationContext ac = new ClassPathXmlApplicationContext(path);
13         Student student = (Student) ac.getBean("myStudent");
14         System.out.println(student);
15 
16     }
17 }

Student{name='张三', age=20, school=School{name='一中', address='莒县'}}

posted @ 2021-02-27 00:34  渐行、渐远  阅读(200)  评论(0)    收藏  举报