一:实验目的

1、了解Spring框架体系结构;

2、理解IoC和DI概念;

3、掌握Bean实例化、装配方式、作用域和生命周期。

# 推荐网课:【黑马程序员2022新版SSM框架教程_Spring+SpringMVC+Maven高级+SpringBoot+MyBatisPlus企业实用开发技术】 https://www.bilibili.com/video/BV1Fi4y1S7ix?p=21&share_source=copy_web&vd_source=8613079943ff0493ec0d6d3985cc70cc

二:实验内容

1、有学生类含学号、姓名、年龄及爱好(可以取多值)属性。请基于XML装配方式分别用构造注入和setter注入创建以下两个实例:

学生1(5,张翔,19,’音乐、篮球、旅游’);

学生2(8,李天尚,20,‘游泳、游戏、阅读’);

# 本实验所需的jar包(联网导入):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>test01</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency></dependencies></project>

# 创建student1(用构造方法注入)的类:

package com.itheima.beans;public class Student1 {private String id;private String name;private int age;private String hobby;public Student1(String id, String name, int age, String hobby) {this.id = id;this.name = name;this.age = age;this.hobby = hobby;}@Overridepublic String toString() {return "Student1{" +"id='" + id + '\'' +", name='" + name + '\'' +", age=" + age +", hobby='" + hobby + '\'' +'}';}
}

# 创建student1的实现类:

import com.itheima.beans.Student1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class student1Test {public static void main(String[] args) {//加载applicationContext.xml配置,相当于获取IOC容器ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//获取配置中的Student1实例,相当于从IOC容器中获取bean(即Student1类)Student1 stu1 = (Student1) ac.getBean("student1");System.out.println(stu1);}}

# 创建student2(用setter方法注入)的类:

package com.itheima.beans;public class Student2 {private String id;private String name;private int age;private String hobby;public Student2() {}@Overridepublic String toString() {return "Student2{" +"id='" + id + '\'' +", name='" + name + '\'' +", age=" + age +", hobby='" + hobby + '\'' +'}';}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;}
}

# 创建student2的实现类:

import com.itheima.beans.Student2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class student2Test {public static void main(String[] args) {//加载applicationContext.xml配置,相当于获取IOC容器ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//获取配置中的Student2实例,相当于从IOC容器中获取bean(即Student2类)Student2 stu2 = (Student2) ac.getBean("student2");System.out.println(stu2);}}

# 配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--    基于XML装配方式中的构造方法注入--><bean id="student1" class="com.itheima.beans.Student1"><constructor-arg name="id" value="5"></constructor-arg><constructor-arg name="name" value="张翔"></constructor-arg><constructor-arg name="age" value="19"></constructor-arg><constructor-arg name="hobby" value="音乐、篮球、旅游"></constructor-arg></bean><!--    基于XML装配方式中的setter方法注入--><bean id="student2" class="com.itheima.beans.Student2"><property name="id" value="8"></property><property name="name" value="李天尚"></property><property name="age" value="20"></property><property name="hobby" value="游泳、游戏、阅读"></property></bean></beans>

2、有教师类含工号,姓名,学历属性。请基于注解方式创建下面实例;

教师(1001,张啸,博士)。

# 创建teacher(用注解的方法注入)类:

package com.itheima.beans;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;@Component("teacher") //指定一个Bean,相当于把teacher类装入IOC容器
@Scope("singleton")
public class Teacher {@Value("1001")private String id;@Value("张啸")private String name;@Value("博士")private String education;@Overridepublic String toString() {return "Teacher{" +"id='" + id + '\'' +", name='" + name + '\'' +", education='" + education + '\'' +'}';}}

# 创建teacher的实现类:

import com.itheima.beans.Teacher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class teacherTest {public static void main(String[] args) {ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");Teacher teacher = (Teacher) ac.getBean("teacher");System.out.println(teacher);}}

# 配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--    基于注解的装配方式--><context:component-scan base-package="com.itheima"></context:component-scan></beans>

实验1 Bean管理相关推荐

  1. IOC操作Bean管理XML方式(外部属性文件)

    目录 IOC操作Bean管理XML方式(外部属性文件) 前情引入: 实验演示: 1.直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池jar包 (3)创建一个bean6.xml配置文件 ...

  2. 【一步一步学习spring】spring bean管理(上)

    1. spring 工厂类 我们前边的demo中用到的spring 工厂类是ClassPathXmlApplicationContext,从上图可以看到他还有一个兄弟类FileSystemApplic ...

  3. linux内存实验,LINUX编程-实验五 内存管理实验

    实验五内存管理实验 1.目的要求 (1)学习使用内存管理库函数. (2)学习分析.改正内存错误. 2.实验内容 (1)内存库函数实验 ●malloc函数 原型:extern void *malloc( ...

  4. spring项目属性注入和bean管理xml 注入一般属性和集合属性

    IOC 介绍: 在Spring的应用中,Spring IoC容器可以创建.装配和配置应用组件对象,这里的组件对象称为Bean. Bean的实例化 在面向对象编程中,想使用某个对象时,需要事先实例化该对 ...

  5. linux进程管理命令实验,实验2Linux进程管理.doc

    实验2Linux进程管理 实验2 Linux进程管理 实验目的 1.加深对进程概念的理解,明确进程和程序的区别 2.进一步认识并发执行的实质 3.分析进程争用资源的现象,学习解决进程互斥的方法 实验性 ...

  6. 2.oracle物理结构,oracle实验2oracle物理结构管理

    oracle实验2oracle物理结构管理 (6页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 9.9 积分 实验2 oracle物理存储结构管理.实验目 ...

  7. IOC操作Bean管理注解方式(完全注解开发)

    IOC操作Bean管理注解方式(完全注解开发) (1)创建配置类,替代xml配置文件 需要让Spring 把一个普通的类认为是配置类 结构图: SpringConfig类代码如下: package c ...

  8. IOC操作Bean管理注解方式(注入属性@Autowired和Qualifier)

    目录 IOC操作Bean管理注解方式(注入属性@Autowired.@Qualifier和@Resource) 1.基于注解方式实现 属性注入 (1)@Autowired:根据属性类型进行自动装配 第 ...

  9. IOC操作Bean管理注解方式(组件扫描配置)

    IOC操作Bean管理注解方式(组件扫描配置) 开启组件扫描的 细节配置 约定那些类可以扫描,哪些类不可以扫描 bean1.xml配置如下: <?xml version="1.0&qu ...

最新文章

  1. 用了这么久配置中心,还不知道长轮询是什么?
  2. java JDK 11.0.5的安装
  3. Asp.net Mvc问题索引
  4. 表格中的border-spacing与border
  5. activity 启动模式_Android世界:Activity的启动模式及其适用范围
  6. 百老汇原版音乐剧《摇滚学校》2月开启中国巡演
  7. x265发起者陈敏:别人看我是专注,其实我只是感兴趣而已
  8. mysql 回滚段_史上最牛分析MySQL索引机制的实现!不接受反驳
  9. asp.net core2.2 多用户验证和授权
  10. [SpringSecurity]web权限方案_用户授权_注解使用
  11. Ubuntu18.04安装CUDA10.1和cuDNN v7.6.5
  12. Python机器学习:Grid SearchCV(网格搜索)
  13. 关于casewhen...的用法
  14. php 队列管理器,Horizon 队列管理工具
  15. 计算机用户原始密码是多少,administrator初始密码是多少
  16. 简单的微信使用技巧,你需要掌握的技巧
  17. Spark 实践——基于 Spark MLlib 和 YFCC 100M 数据集的景点推荐系统
  18. 推荐两个BlackBerry 商务软件
  19. 华为OD机试真题 C++ 实现【异常的打卡记录】【2022.11 Q4 新题】
  20. 《STM32从零开始学习历程》——I2C固件库

热门文章

  1. 2023年全国最新工会考试精选真题及答案41
  2. P1002 过河卒(dp动态规划,洛谷,java)
  3. 思迅软锁安装配置说明
  4. Latex自定义文档纸张大小
  5. 蓝牙开发那些事儿(3)——看看空中包
  6. 共轭相似以及共轭对角化
  7. 使用C#从图片文件生成图标ICON文件(附源文件)
  8. 【系统分析师之路】第七章 复盘系统设计(业务流程建模)
  9. 艾永亮:为什么良品铺子能在同质化的零食市场中突出重围?
  10. 云班课python答案_云班课测试题答案