1.4 HelloWorld
- 目标:使用Spring创建对象,为属性赋值
- 创建Student类
- 创建Spring配置文件
<!-- 使用bean元素定义一个由IOC容器创建的对象 --> <!-- class属性指定用于创建bean的全类名 --> <!-- id属性指定用于引用bean实例的标识 --> <bean id="student" class="com.atguigu.helloworld.bean.Student"> <!-- 使用property子元素为bean的属性赋值 --> <property name="studentId" value="1001"/> <property name="stuName" value="Tom2015"/> <property name="age" value="20"/> </bean> |
- 测试:通过Spring的IOC容器创建Student类实例
//1.创建IOC容器对象 ApplicationContext iocContainer = new ClassPathXmlApplicationContext("helloworld.xml"); //2.根据id值获取bean实例对象 Student student = (Student) iocContainer.getBean("student"); //3.打印bean System.out.println(student); |
本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。