手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

Spring入门实例:用Spring创建对象

时间:2024-10-13 20:59:19

1、新建一个domain对象实例:Department。package com.gwolf.springmvc.domain;publ足毂忍珩ic class Department { private Integer id; private String departmentName; public Department() { } public Department(int i, String string) { this.id = i; this.departmentName = string; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Override public String toString() { return "Department [id=" + id + ", departmentName=" + departmentName + "]"; } }

Spring入门实例:用Spring创建对象

2、在java类路径下新建一个文件:applicationContext.xml。

Spring入门实例:用Spring创建对象

3、在applicationContext.xml文件中声明Department实例。争犸禀淫<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="department" class = "com.gwolf.springmvc.domain.Department"> <property name="departmentName" value="技术部"></property> </bean> </beans>

Spring入门实例:用Spring创建对象

4、在Main类中初始化Spring的IOC容器对象。ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

Spring入门实例:用Spring创建对象

5、从IOC容器中获取Bean实例:Spring将调用构造器初始化Bean方法。ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Department department = (Department)applicationContext.getBean("department");

Spring入门实例:用Spring创建对象

6、调用Bean对象的方法。

Spring入门实例:用Spring创建对象
© 手抄报圈