文章目录
前言
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() { } publicStringtoString() { 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标签必须引入相应的命名空间
<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命名空间
<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 名称空间
<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>
⑤测试
publicvoidtestDataSource() throwsSQLException { ApplicationContextac=newClassPathXmlApplicationContext("spring-datasource.xml"); DataSourcedataSource=ac.getBean(DataSource.class); Connectionconnection=dataSource.getConnection(); System.out.println(connection); }
总结
以上就是Spring之容器:IOC(2)的相关知识点,希望对你有所帮助。
积跬步以至千里,积怠惰以至深渊。时代在这跟着你一起努力哦!