Spring之容器:IOC(2)

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
可观测可视化 Grafana 版,10个用户账号 1个月
简介: 【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)的相关知识点,希望对你有所帮助。

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
Java 测试技术 开发工具
ApplicationArguments读取应用程序参数并注入到IOC容器
ApplicationArguments读取应用程序参数并注入到IOC容器
ApplicationArguments读取应用程序参数并注入到IOC容器
|
3天前
|
Java Spring 容器
【Spring系列笔记】IOC与DI
IoC 和 DI 是面向对象编程中的两个相关概念,它们主要用于解决程序中的依赖管理和解耦问题。 控制反转是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入和依赖查找。
19 2
|
3天前
|
Java 测试技术 数据库连接
Spring中ioc的优点
总之,Spring中的IoC提供了一种更加灵活、可维护、可测试和可扩展的方式来管理组件之间的依赖关系,从而提高了应用程序的质量和可维护性。这使得开发人员能够更专注于业务逻辑而不是底层的技术细节。
22 1
|
4天前
|
XML Java 数据格式
手写spring第八章-定义标记类型Aware接口,实现感知容器对象
手写spring第八章-定义标记类型Aware接口,实现感知容器对象
4 0
|
4天前
|
设计模式 存储 Java
手写spring第二章-运用设计模式编写可扩展的容器
手写spring第二章-运用设计模式编写可扩展的容器
7 0
|
4天前
|
XML 缓存 Java
Spring核心功能IOC详解
Spring核心功能IOC详解
15 0
Spring核心功能IOC详解
|
6天前
|
XML Java 数据格式
【spring】01 Spring容器研究
【spring】01 Spring容器研究
8 0
|
27天前
|
XML Java 数据格式
Spring(一)IOC小案例
Spring(一)IOC小案例
|
2月前
|
消息中间件 前端开发 Java
面试官:说说Spring中IoC实现原理?
IoC(控制反转)是种编程思想,旨在降低代码耦合,提高重用性、可测试性和灵活性。Spring通过工厂模式和反射实现IoC,其中依赖注入(DI)是常见实现方式。初始化IoC容器,读取配置文件创建Bean并利用反射加载对象。课后思考涉及工厂模式在Spring源码中的体现及反射核心实现。更多内容见[www.javacn.site](https://www.javacn.site)。
18 1
|
2月前
|
存储 前端开发 Java
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext