Spring之容器:IOC(2)

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
MSE Nacos/ZooKeeper 企业版试用,1600元额度,限量50份
简介: 【1月更文挑战第14天】7、实验六:为数组类型属性赋值8、实验七:为集合类型属性赋值①为List集合类型属性赋值②为Map集合类型属性赋值③引用集合类型的bean9、实验八:p命名空间10、实验九:引入外部属性文件7、实验六:为数组类型属性赋值8、实验七:为集合类型属性赋值①为List集合类型属性赋值②为Map集合类型属性赋值③引用集合类型的bean9、实验八:p命名空间10、实验九:引入外部属性文件

文章目录

前言

7、实验六:为数组类型属性赋值

8、实验七:为集合类型属性赋值

①为List集合类型属性赋值

②为Map集合类型属性赋值

③引用集合类型的bean

9、实验八:p命名空间

10、实验九:引入外部属性文件

总结


前言

7、实验六:为数组类型属性赋值

8、实验七:为集合类型属性赋值

①为List集合类型属性赋值

②为Map集合类型属性赋值

③引用集合类型的bean

9、实验八:p命名空间

10、实验九:引入外部属性文件

7、实验六:为数组类型属性赋值

8、实验七:为集合类型属性赋值

①为List集合类型属性赋值

②为Map集合类型属性赋值

③引用集合类型的bean

9、实验八:p命名空间

10、实验九:引入外部属性文件


7、实验六:为数组类型属性赋值

①修改Student类

在Student类中添加以下代码:

privateString[] hobbies;
publicString[] getHobbies() {
returnhobbies;
}
publicvoidsetHobbies(String[] hobbies) {
this.hobbies=hobbies;
}

②配置bean

<beanid="studentFour"class="com.gedeshidai.spring.bean6.Student"><propertyname="id"value="1004"></property><propertyname="name"value="赵六"></property><propertyname="age"value="26"></property><propertyname="sex"value="女"></property><!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><propertyname="clazz"ref="clazzOne"></property><propertyname="hobbies"><array><value>抽烟</value><value>喝酒</value><value>烫头</value></array></property></bean>

8、实验七:为集合类型属性赋值

①为List集合类型属性赋值

在Clazz类中添加以下代码:

privateList<Student>students;
publicList<Student>getStudents() {
returnstudents;
}
publicvoidsetStudents(List<Student>students) {
this.students=students;
}

配置bean:

<beanid="clazzTwo"class="com.gedeshidai.spring6.bean.Clazz"><propertyname="clazzId"value="4444"></property><propertyname="clazzName"value="Javaee0222"></property><propertyname="students"><list><refbean="studentOne"></ref><refbean="studentTwo"></ref><refbean="studentThree"></ref></list></property></bean>

若为Set集合类型属性赋值,只需要将其中的list标签改为set标签即可

②为Map集合类型属性赋值

创建教师类Teacher:

packagecom.gedeshidai.spring6.bean;
publicclassTeacher {
privateIntegerteacherId;
privateStringteacherName;
publicIntegergetTeacherId() {
returnteacherId;
    }
publicvoidsetTeacherId(IntegerteacherId) {
this.teacherId=teacherId;
    }
publicStringgetTeacherName() {
returnteacherName;
    }
publicvoidsetTeacherName(StringteacherName) {
this.teacherName=teacherName;
    }
publicTeacher(IntegerteacherId, StringteacherName) {
this.teacherId=teacherId;
this.teacherName=teacherName;
    }
publicTeacher() {
    }
@OverridepublicStringtoString() {
return"Teacher{"+"teacherId="+teacherId+", teacherName='"+teacherName+'\''+'}';
    }
}

在Student类中添加以下代码:

privateMap<String, Teacher>teacherMap;
publicMap<String, Teacher>getTeacherMap() {
returnteacherMap;
}
publicvoidsetTeacherMap(Map<String, Teacher>teacherMap) {
this.teacherMap=teacherMap;
}

配置bean:

<beanid="teacherOne"class="com.atguigu.spring6.bean.Teacher"><propertyname="teacherId"value="10010"></property><propertyname="teacherName"value="大宝"></property></bean><beanid="teacherTwo"class="com.gedeshidaia.spring6.bean.Teacher"><propertyname="teacherId"value="10086"></property><propertyname="teacherName"value="二宝"></property></bean><beanid="studentFour"class="com.atguigu.spring6.bean.Student"><propertyname="id"value="1004"></property><propertyname="name"value="赵六"></property><propertyname="age"value="26"></property><propertyname="sex"value="女"></property><!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><propertyname="clazz"ref="clazzOne"></property><propertyname="hobbies"><array><value>抽烟</value><value>喝酒</value><value>烫头</value></array></property><propertyname="teacherMap"><map><entry><key><value>10010</value></key><refbean="teacherOne"></ref></entry><entry><key><value>10086</value></key><refbean="teacherTwo"></ref></entry></map></property></bean>

③引用集合类型的bean

<!--list集合类型的bean--><util:listid="students"><refbean="studentOne"></ref><refbean="studentTwo"></ref><refbean="studentThree"></ref></util:list><!--map集合类型的bean--><util:mapid="teacherMap"><entry><key><value>10010</value></key><refbean="teacherOne"></ref></entry><entry><key><value>10086</value></key><refbean="teacherTwo"></ref></entry></util:map><beanid="clazzTwo"class="com.atguigugu.spring6.bean.Clazz"><propertyname="clazzId"value="4444"></property><propertyname="clazzName"value="Javaee0222"></property><propertyname="students"ref="students"></property></bean><beanid="studentFour"class="com.gedeshidai.spring6.bean.Student"><propertyname="id"value="1004"></property><propertyname="name"value="赵六"></property><propertyname="age"value="26"></property><propertyname="sex"value="女"></property><!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><propertyname="clazz"ref="clazzOne"></property><propertyname="hobbies"><array><value>抽烟</value><value>喝酒</value><value>烫头</value></array></property><propertyname="teacherMap"ref="teacherMap"></property></bean>

使用util:list、util:map标签必须引入相应的命名空间

<?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

9、实验八:p命名空间

引入p命名空间

<?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

引入p命名空间后,可以通过以下方式为bean的各个属性赋值

<beanid="studentSix"class="com.atguigu.spring6.bean.Student"p:id="1006"p:name="小明"p:clazz-ref="clazzOne"p:teacherMap-ref="teacherMap"></bean>

10、实验九:引入外部属性文件

①加入依赖

<!-- MySQL驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><!-- 数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.15</version></dependency>

②创建外部属性文件

jdbc.user=root
jdbc.password=gedeshidai
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver

③引入属性文件

引入context 名称空间

<?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"></beans>
<!-- 引入外部属性文件 --><context:property-placeholderlocation="classpath:jdbc.properties"/>

注意:在使用 context:property-placeholder 元素加载外包配置文件功能前,首先需要在 XML 配置的一级标签 中添加 context 相关的约束。

④配置bean

<beanid="druidDataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="url"value="${jdbc.url}"/><propertyname="driverClassName"value="${jdbc.driver}"/><propertyname="username"value="${jdbc.user}"/><propertyname="password"value="${jdbc.password}"/></bean>

⑤测试

@TestpublicvoidtestDataSource() throwsSQLException {
ApplicationContextac=newClassPathXmlApplicationContext("spring-datasource.xml");
DataSourcedataSource=ac.getBean(DataSource.class);
Connectionconnection=dataSource.getConnection();
System.out.println(connection);
}

总结

以上就是Spring之容器:IOC(2)的相关知识点,希望对你有所帮助。

积跬步以至千里,积怠惰以至深渊。时代在这跟着你一起努力哦!

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
7月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
504 26
|
4月前
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
|
9月前
|
XML Java 数据格式
【SpringFramework】Spring IoC-基于XML的实现
本文主要讲解SpringFramework中IoC和DI相关概念,及基于XML的实现方式。
192 69
|
6月前
|
Java 容器 Spring
什么是Spring IOC 和DI ?
IOC : 控制翻转 , 它把传统上由程序代码直接操控的对象的调用权交给容 器,通过容器来实现对象组件的装配和管理。所谓的“控制反转”概念就是对组件对象控制权的转 移,从程序代码本身转移到了外部容器。 DI : 依赖注入,在我们创建对象的过程中,把对象依赖的属性注入到我们的类中。
|
7月前
|
XML Java 数据格式
Spring容器的本质
本文主要讨论Spring容器最核心的机制,用最少的代码讲清楚Spring容器的本质。
|
9月前
|
设计模式 XML Java
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
本文详细介绍了Spring框架的核心功能,并通过手写自定义Spring框架的方式,深入理解了Spring的IOC(控制反转)和DI(依赖注入)功能,并且学会实际运用设计模式到真实开发中。
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
|
9月前
|
Java Spring 容器
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
142 21
|
9月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
225 12
|
9月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
254 6
|
9月前
|
存储 缓存 Java
Spring面试必问:手写Spring IoC 循环依赖底层源码剖析
在Spring框架中,IoC(Inversion of Control,控制反转)是一个核心概念,它允许容器管理对象的生命周期和依赖关系。然而,在实际应用中,我们可能会遇到对象间的循环依赖问题。本文将深入探讨Spring如何解决IoC中的循环依赖问题,并通过手写源码的方式,让你对其底层原理有一个全新的认识。
204 2