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创建对象
  • 花生棒+树莓派端口映射搭建Web网站
  • win10 1903新bug移动宽带无法正常WiFi连接
  • linux下telnet安装与使用
  • 如何让笔记本电脑变成一个路由器
  • Xenserver中如何关闭虚拟机
  • 热门搜索
    生活用品清单 如何让自己变得幽默 三黄鸡是什么鸡 服务器端口怎么看 如何制作微课 抬头纹很重怎么办 桃花劫是什么意思 如何去皱纹 如何减小腿 明星教你如何接吻