• (续)SSM整合之spring笔记(IOC 依赖注入之为属性赋值)(P073—P077)


    2.7  实验六:依赖注入之为类类型属性赋值

    模拟班级和学生  一对多的关系

    ①创建班级类Clazz

    1. package com.atguigu.spring.pojo;
    2. public class Clazz {
    3. private Integer cid;
    4. private String cname;
    5. public Clazz() {
    6. }
    7. public Clazz(Integer cid, String cname) {
    8. this.cid = cid;
    9. this.cname = cname;
    10. }
    11. public Integer getCid() {
    12. return cid;
    13. }
    14. public void setCid(Integer cid) {
    15. this.cid = cid;
    16. }
    17. public String getCname() {
    18. return cname;
    19. }
    20. public void setCname(String cname) {
    21. this.cname = cname;
    22. }
    23. @Override
    24. public String toString() {
    25. return "Clazz{" +
    26. "cid=" + cid +
    27. ", cname='" + cname + '\'' +
    28. '}';
    29. }
    30. }

    ②修改Student

    通过实体类来表示学生和班级之间的关系

        对一:对应的是对象   对多:对应的是集合

    具体做法:

    在学生(多)里面设置一个班级(一)类型的属性  在设置get/set方法  重写tostring方法

    1. package com.atguigu.spring.pojo;
    2. public class Student implements Person {
    3. private Integer sid;
    4. private String sname;
    5. private Integer age;
    6. private String gender;
    7. private Double score;
    8. private Clazz clazz;
    9. public Student() {
    10. }
    11. public Student(Integer sid, String sname ,String gender , Integer age ) {
    12. this.sid = sid;
    13. this.sname = sname;
    14. this.age = age;
    15. this.gender = gender;
    16. }
    17. public Student(Integer sid, String sname, String gender, Double score) {
    18. this.sid = sid;
    19. this.sname = sname;
    20. this.gender = gender;
    21. this.score = score;
    22. }
    23. public Integer getSid() {
    24. return sid;
    25. }
    26. public void setSid(Integer sid) {
    27. this.sid = sid;
    28. }
    29. public String getSname() {
    30. return sname;
    31. }
    32. public void setSname(String sname) {
    33. this.sname = sname;
    34. }
    35. public Integer getAge() {
    36. return age;
    37. }
    38. public void setAge(Integer age) {
    39. this.age = age;
    40. }
    41. public String getGender() {
    42. return gender;
    43. }
    44. public void setGender(String gender) {
    45. this.gender = gender;
    46. }
    47. public Double getScore() {
    48. return score;
    49. }
    50. public void setScore(Double score) {
    51. this.score = score;
    52. }
    53. public Clazz getClazz() {
    54. return clazz;
    55. }
    56. public void setClazz(Clazz clazz) {
    57. this.clazz = clazz;
    58. }
    59. @Override
    60. public String toString() {
    61. return "Student{" +
    62. "sid=" + sid +
    63. ", sname='" + sname + '\'' +
    64. ", age=" + age +
    65. ", gender='" + gender + '\'' +
    66. ", score=" + score +
    67. ", clazz=" + clazz +
    68. '}';
    69. }
    70. }

    不能够用value赋值 因为value对应的是字面量 而clazz 不是字面量,clazz对应的是一个对象

    ③给类类型赋值的三种方式

    我们要给类类型赋值的话 我们有三种方式:

    方式一:引用外部已声明的 bean
    方式二:内部 bean
    方式三:级联属性赋值

    ①方式一:引用外部已声明的bean

    配置 Clazz 类型的 bean
    1. <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    2. <property name="cid" value="1111">property>
    3. <property name="cname" value="最强王者班">property>
    4. bean>
    Student 中的 clazz 属性赋值:
    1. <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    2. <property name="sid" value="1004">property>
    3. <property name="sname" value="赵六">property>
    4. <property name="age" value="26">property>
    5. <property name="gender" value="男">property>
    6. <property name="clazz" ref="clazzOne">property>
    7. bean>

    合起来:

    1. <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    2. <property name="sid" value="1004">property>
    3. <property name="sname" value="赵六">property>
    4. <property name="age" value="26">property>
    5. <property name="gender" value="男">property>
    6. <property name="clazz" ref="clazzOne">property>
    7. bean>
    8. <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    9. <property name="cid" value="1111">property>
    10. <property name="cname" value="最强王者班">property>
    11. bean>

    测试

    1. @Test
    2. public void testDI(){
    3. //获取IOC容器
    4. ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    5. //获取bean
    6. Student student = ioc.getBean("studentFive", Student.class);
    7. System.out.println(student);
    8. }

    ② 方式二:级联属性赋值(用得不多)   2222,远大前程班会覆盖   11111 最强王者班 

    1. <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    2. <property name="sid" value="1004">property>
    3. <property name="sname" value="赵六">property>
    4. <property name="age" value="26">property>
    5. <property name="gender" value="男">property>
    6. <property name="clazz" ref="clazzOne">property>
    7. <property name="clazz.cid" value="2222">property>
    8. <property name="clazz.cname" value="远大前程班">property>
    9. bean>
    10. <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    11. <property name="cid" value="1111">property>
    12. <property name="cname" value="最强王者班">property>
    13. bean>

    测试

    1. @Test
    2. public void testDI(){
    3. //获取IOC容器
    4. ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    5. //获取bean
    6. Student student = ioc.getBean("studentFive", Student.class);
    7. System.out.println(student);
    8. }

    方式三 :内部bean

    1. <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    2. <property name="sid" value="1004">property>
    3. <property name="sname" value="赵六">property>
    4. <property name="age" value="26">property>
    5. <property name="gender" value="男">property>
    6. <property name="clazz">
    7. <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
    8. <property name="cid" value="2222">property>
    9. <property name="cname" value="远大前程班">property>
    10. bean>
    11. property>
    12. bean>
    13. <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    14. <property name="cid" value="1111">property>
    15. <property name="cname" value="最强王者班">property>
    16. bean>

    测试:

    1. @Test
    2. public void testDI(){
    3. //获取IOC容器
    4. ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    5. //获取bean
    6. Student student = ioc.getBean("studentFive", Student.class);
    7. System.out.println(student);
    8. }

    内部bean能够通过ioc直接获取获取吗?

    不能,内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取

    测试:

    1. @Test
    2. public void testDI(){
    3. //获取IOC容器
    4. ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    5. //获取bean
    6. // Student student = ioc.getBean("studentTwo", Student.class);
    7. Student student = ioc.getBean("studentFive", Student.class);
    8. Clazz clazz = ioc.getBean("clazzInner", Clazz.class);
    9. System.out.println(clazz);
    10. }

     报错:

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'clazzInner' available

    没有一个bean叫做clazzInner被发现  没有一个叫做clazzInner的bean可用 

    available:可用

    2. 8 实验七:为数组类型属性赋值

    ①修改Student

    1. package com.atguigu.spring.pojo;
    2. import java.util.Arrays;
    3. public class Student implements Person {
    4. private Integer sid;
    5. private String sname;
    6. private Integer age;
    7. private String gender;
    8. private Double score;
    9. private Clazz clazz;
    10. private String[] hobby;
    11. public Student() {
    12. }
    13. public Student(Integer sid, String sname ,String gender , Integer age ) {
    14. this.sid = sid;
    15. this.sname = sname;
    16. this.age = age;
    17. this.gender = gender;
    18. }
    19. public Student(Integer sid, String sname, String gender, Double score) {
    20. this.sid = sid;
    21. this.sname = sname;
    22. this.gender = gender;
    23. this.score = score;
    24. }
    25. public Integer getSid() {
    26. return sid;
    27. }
    28. public void setSid(Integer sid) {
    29. this.sid = sid;
    30. }
    31. public String getSname() {
    32. return sname;
    33. }
    34. public void setSname(String sname) {
    35. this.sname = sname;
    36. }
    37. public Integer getAge() {
    38. return age;
    39. }
    40. public void setAge(Integer age) {
    41. this.age = age;
    42. }
    43. public String getGender() {
    44. return gender;
    45. }
    46. public void setGender(String gender) {
    47. this.gender = gender;
    48. }
    49. public Double getScore() {
    50. return score;
    51. }
    52. public void setScore(Double score) {
    53. this.score = score;
    54. }
    55. public Clazz getClazz() {
    56. return clazz;
    57. }
    58. public void setClazz(Clazz clazz) {
    59. this.clazz = clazz;
    60. }
    61. public String[] getHobby() {
    62. return hobby;
    63. }
    64. public void setHobby(String[] hobby) {
    65. this.hobby = hobby;
    66. }
    67. @Override
    68. public String toString() {
    69. return "Student{" +
    70. "sid=" + sid +
    71. ", sname='" + sname + '\'' +
    72. ", age=" + age +
    73. ", gender='" + gender + '\'' +
    74. ", score=" + score +
    75. ", clazz=" + clazz +
    76. ", hobby=" + Arrays.toString(hobby) +
    77. '}';
    78. }
    79. }

    ②配置bean

    1. <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    2. <property name="sid" value="1004">property>
    3. <property name="sname" value="赵六">property>
    4. <property name="age" value="26">property>
    5. <property name="gender" value="男">property>
    6. <property name="clazz">
    7. <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
    8. <property name="cid" value="2222">property>
    9. <property name="cname" value="远大前程班">property>
    10. bean>
    11. property>
    12. <property name="hobby">
    13. <array>
    14. <value>抽烟value>
    15. <value>喝酒value>
    16. <value>烫头value>
    17. array>
    18. property>
    19. bean>
    20. <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    21. <property name="cid" value="1111">property>
    22. <property name="cname" value="最强王者班">property>
    23. bean>

     测试:

     2.9 实验八:为集合类型属性赋值

    ①为List集合类型属性赋值

    Clazz 类中添加以下代码:
    1. package com.atguigu.spring.pojo;
    2. import java.util.List;
    3. public class Clazz {
    4. private Integer cid;
    5. private String cname;
    6. private List students;
    7. public Clazz() {
    8. }
    9. public Clazz(Integer cid, String cname) {
    10. this.cid = cid;
    11. this.cname = cname;
    12. }
    13. public Integer getCid() {
    14. return cid;
    15. }
    16. public void setCid(Integer cid) {
    17. this.cid = cid;
    18. }
    19. public String getCname() {
    20. return cname;
    21. }
    22. public void setCname(String cname) {
    23. this.cname = cname;
    24. }
    25. public List getStudents() {
    26. return students;
    27. }
    28. public void setStudents(List students) {
    29. this.students = students;
    30. }
    31. @Override
    32. public String toString() {
    33. return "Clazz{" +
    34. "cid=" + cid +
    35. ", cname='" + cname + '\'' +
    36. ", students=" + students +
    37. '}';
    38. }
    39. }
    配置 bean
    1. <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    2. <property name="cid" value="1111">property>
    3. <property name="cname" value="最强王者班">property>
    4. <property name="students">
    5. <list>
    6. <ref bean="studentOne">ref>
    7. <ref bean="studentTwo">ref>
    8. <ref bean="studentThree">ref>
    9. list>
    10. property>
    11. bean>

    测试:

    1. @Test
    2. public void testDI(){
    3. //获取IOC容器
    4. ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    5. //获取bean
    6. Clazz clazz = ioc.getBean("clazzOne", Clazz.class);
    7. System.out.println(clazz);

     另一种方式    配置一个集合类型的bean,需要使用util的约束

     

    xmlns:util="http://www.springframework.org/schema/util"
    1. <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    2. <property name="cid" value="1111">property>
    3. <property name="cname" value="最强王者班">property>
    4. <property name="students" ref="studentList">property>
    5. bean>
    6. <util:list id="studentList">
    7. <ref bean="studentOne">ref>
    8. <ref bean="studentTwo">ref>
    9. <ref bean="studentThree">ref>
    10. util:list>

    测试

    1. @Test
    2. public void testDI(){
    3. //获取IOC容器
    4. ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    5. //获取bean
    6. Clazz clazz = ioc.getBean("clazzOne", Clazz.class);
    7. System.out.println(clazz);

     

    ②为Map集合类型属性赋值

    创建教师类 Teacher
    1. package com.atguigu.spring.pojo;
    2. /**
    3. * Date:2022/7/1
    4. * Author:ybc
    5. * Description:
    6. */
    7. public class Teacher {
    8. private Integer tid;
    9. private String tname;
    10. @Override
    11. public String toString() {
    12. return "Teacher{" +
    13. "tid=" + tid +
    14. ", tname='" + tname + '\'' +
    15. '}';
    16. }
    17. public Integer getTid() {
    18. return tid;
    19. }
    20. public void setTid(Integer tid) {
    21. this.tid = tid;
    22. }
    23. public String getTname() {
    24. return tname;
    25. }
    26. public void setTname(String tname) {
    27. this.tname = tname;
    28. }
    29. public Teacher() {
    30. }
    31. public Teacher(Integer tid, String tname) {
    32. this.tid = tid;
    33. this.tname = tname;
    34. }
    35. }
    Student 类中添加以下代码:

     

    1. package com.atguigu.spring.pojo;
    2. import java.util.Arrays;
    3. import java.util.Map;
    4. public class Student implements Person {
    5. private Integer sid;
    6. private String sname;
    7. private Integer age;
    8. private String gender;
    9. private Double score;
    10. private Clazz clazz;
    11. private String[] hobby;
    12. private Map teacherMap;
    13. public Student() {
    14. }
    15. public Student(Integer sid, String sname ,String gender , Integer age ) {
    16. this.sid = sid;
    17. this.sname = sname;
    18. this.age = age;
    19. this.gender = gender;
    20. }
    21. public Student(Integer sid, String sname, String gender, Double score) {
    22. this.sid = sid;
    23. this.sname = sname;
    24. this.gender = gender;
    25. this.score = score;
    26. }
    27. public Integer getSid() {
    28. return sid;
    29. }
    30. public void setSid(Integer sid) {
    31. this.sid = sid;
    32. }
    33. public String getSname() {
    34. return sname;
    35. }
    36. public void setSname(String sname) {
    37. this.sname = sname;
    38. }
    39. public Integer getAge() {
    40. return age;
    41. }
    42. public void setAge(Integer age) {
    43. this.age = age;
    44. }
    45. public String getGender() {
    46. return gender;
    47. }
    48. public void setGender(String gender) {
    49. this.gender = gender;
    50. }
    51. public Double getScore() {
    52. return score;
    53. }
    54. public void setScore(Double score) {
    55. this.score = score;
    56. }
    57. public Clazz getClazz() {
    58. return clazz;
    59. }
    60. public void setClazz(Clazz clazz) {
    61. this.clazz = clazz;
    62. }
    63. public String[] getHobby() {
    64. return hobby;
    65. }
    66. public void setHobby(String[] hobby) {
    67. this.hobby = hobby;
    68. }
    69. public Map getTeacherMap() {
    70. return teacherMap;
    71. }
    72. public void setTeacherMap(Map teacherMap) {
    73. this.teacherMap = teacherMap;
    74. }
    75. @Override
    76. public String toString() {
    77. return "Student{" +
    78. "sid=" + sid +
    79. ", sname='" + sname + '\'' +
    80. ", age=" + age +
    81. ", gender='" + gender + '\'' +
    82. ", score=" + score +
    83. ", clazz=" + clazz +
    84. ", hobby=" + Arrays.toString(hobby) +
    85. ", teacherMap=" + teacherMap +
    86. '}';
    87. }
    88. }
    第一种方式: 配置 bean
    1. <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    2. <property name="sid" value="1004">property>
    3. <property name="sname" value="赵六">property>
    4. <property name="age" value="26">property>
    5. <property name="gender" value="男">property>
    6. <property name="clazz">
    7. <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
    8. <property name="cid" value="2222">property>
    9. <property name="cname" value="远大前程班">property>
    10. bean>
    11. property>
    12. <property name="hobby">
    13. <array>
    14. <value>抽烟value>
    15. <value>喝酒value>
    16. <value>烫头value>
    17. array>
    18. property>
    19. <property name="teacherMap">
    20. <map>
    21. <entry key="10086" value-ref="teacherOne">entry>
    22. <entry key="10010" value-ref="teacherTwo">entry>
    23. map>
    24. property>
    25. bean>
    26. <bean id="teacherOne" class="com.atguigu.spring.pojo.Teacher">
    27. <property name="tid" value="10086">property>
    28. <property name="tname" value="大宝">property>
    29. bean>
    30. <bean id="teacherTwo" class="com.atguigu.spring.pojo.Teacher">
    31. <property name="tid" value="10010">property>
    32. <property name="tname" value="小宝">property>
    33. bean>
    34. beans>
    测试
    1. @Test
    2. public void testDI(){
    3. //获取IOC容器
    4. ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    5. //获取bean
    6. Student student = ioc.getBean("studentFive", Student.class);
    7. System.out.println(student);
    8. }

     

    第二种方式: 配置 bean
    1. <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    2. <property name="sid" value="1004">property>
    3. <property name="sname" value="赵六">property>
    4. <property name="age" value="26">property>
    5. <property name="gender" value="男">property>
    6. <property name="clazz">
    7. <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
    8. <property name="cid" value="2222">property>
    9. <property name="cname" value="远大前程班">property>
    10. bean>
    11. property>
    12. <property name="hobby">
    13. <array>
    14. <value>抽烟value>
    15. <value>喝酒value>
    16. <value>烫头value>
    17. array>
    18. property>
    19. <property name="teacherMap" ref="teacherMap">property>
    20. bean>
    21. <bean id="teacherOne" class="com.atguigu.spring.pojo.Teacher">
    22. <property name="tid" value="10086">property>
    23. <property name="tname" value="大宝">property>
    24. bean>
    25. <bean id="teacherTwo" class="com.atguigu.spring.pojo.Teacher">
    26. <property name="tid" value="10010">property>
    27. <property name="tname" value="小宝">property>
    28. bean>
    29. <util:map id="teacherMap">
    30. <entry key="10086" value-ref="teacherOne">entry>
    31. <entry key="10010" value-ref="teacherTwo">entry>
    32. util:map>
    1. @Test
    2. public void testDI(){
    3. //获取IOC容器
    4. ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    5. //获取bean
    6. Student student = ioc.getBean("studentFive", Student.class);
    7. System.out.println(student);
    8. }

     

  • 相关阅读:
    Prometheus系列第十篇一核心之micrometer源码分析一micrometer-registry-prometheus核心实现
    提升网站效率与SEO优化:ZBlog插件集成解决方案
    深入理解算法:从基础到实践
    除了 MySQL,这些数据库你都认识么?
    MySQL学习笔记
    运营商sdwan优缺点及sdwan服务商优势
    航天常用术语-双想、九新、六个百分百确认、三图、三类关键特性、可靠性1+6+2、质量问题归零、技术状态更改“五条”原则、四随
    vue.config.js忽略对eslint检查
    虚拟机用户切换及设置root权限的密码
    自动化测试教程(20)了解PageObject模式
  • 原文地址:https://blog.csdn.net/m0_59281987/article/details/127605000