4、Spring之依赖注入

打印 上一主题 下一主题

主题 907|帖子 907|积分 2721

依赖注入就是对类的属性进行赋值
4.1、环境搭建

创建名为spring_ioc_xml的新module,过程参考3.1节
4.1.1、创建spring配置文件

  1. [/code][size=4]4.1.2、创建学生类Student[/size]
  2. [img]https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727223544516-1576170602.png[/img]
  3. [code]package org.rain.spring.pojo;
  4. /**
  5. * @author liaojy
  6. * @date 2023/7/27 - 22:33
  7. */
  8. public class Student {
  9.     private Integer id;
  10.     private String name;
  11.     private Integer age;
  12.     private String sex;
  13.     public Student() {
  14.     }
  15.     public Student(Integer id, String name, Integer age, String sex) {
  16.         this.id = id;
  17.         this.name = name;
  18.         this.age = age;
  19.         this.sex = sex;
  20.     }
  21.     public Integer getId() {
  22.         return id;
  23.     }
  24.     public void setId(Integer id) {
  25.         this.id = id;
  26.     }
  27.     public String getName() {
  28.         return name;
  29.     }
  30.     public void setName(String name) {
  31.         this.name = name;
  32.     }
  33.     public Integer getAge() {
  34.         return age;
  35.     }
  36.     public void setAge(Integer age) {
  37.         this.age = age;
  38.     }
  39.     public String getSex() {
  40.         return sex;
  41.     }
  42.     public void setSex(String sex) {
  43.         this.sex = sex;
  44.     }
  45.     @Override
  46.     public String toString() {
  47.         return "Student{" +
  48.                 "id=" + id +
  49.                 ", name='" + name + '\'' +
  50.                 ", age=" + age +
  51.                 ", sex='" + sex + '\'' +
  52.                 '}';
  53.     }
  54. }
复制代码
4.2、setter注入(常用)

4.2.1、配置bean

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>
复制代码
4.2.2、测试

  1. package org.rain.spring.test;
  2. import org.junit.Test;
  3. import org.rain.spring.pojo.Student;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. /**
  7. * @author liaojy
  8. * @date 2023/7/27 - 22:43
  9. */
  10. public class IOCByXmlTest {
  11.     @Test
  12.     public void testDISetter(){
  13.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  14.         Student student = applicationContext.getBean("student", Student.class);
  15.         System.out.println(student);
  16.     }
  17. }
复制代码
4.3、构造器注入

4.3.1、配置bean


注意:constructor-arg标签的数量,必须和某一个构造器方法的参数数量一致
  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>
复制代码
4.3.2、测试

  1.     @Test
  2.     public void testDIConstructor(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentTwo", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
4.4、特殊值处理

4.4.1、null值

4.4.1.1、配置bean


注意:该写法,实际为sex所赋的值是字符串null
  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>        
复制代码
4.4.1.2、测试


由控制台日志可知,此时sex的值为null
  1.     @Test
  2.     public void testDIspecial(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentThree", Student.class);
  5.         System.out.println(student.getSex().toString());
  6.     }
复制代码
+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

由控制台日志可知,此时age的值也为null;所以不配置属性也能实现同样的效果
4.4.2、xml字符值

4.4.2.1、方式一:实体

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>               
复制代码
4.4.2.2、方式二:CDATA节

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>            ]]>    <bean id="clazz" >
  8.         <property name="cid" value="1111"></property>
  9.         <property name="cname" value="三年E班"></property>
  10.     </bean>               
复制代码
4.4.2.3、测试


4.5、为类类型的属性赋值

4.5.1、方式一:外部bean(常用)

4.5.1.1、创建班级类Clazz

  1. package org.rain.spring.pojo;
  2. /**
  3. * @author liaojy
  4. * @date 2023/7/28 - 7:54
  5. */
  6. public class Clazz {
  7.     private Integer cid;
  8.     private String cname;
  9.     public Clazz() {
  10.     }
  11.     public Clazz(Integer cid, String cname) {
  12.         this.cid = cid;
  13.         this.cname = cname;
  14.     }
  15.     public Integer getCid() {
  16.         return cid;
  17.     }
  18.     public void setCid(Integer cid) {
  19.         this.cid = cid;
  20.     }
  21.     public String getCname() {
  22.         return cname;
  23.     }
  24.     public void setCname(String cname) {
  25.         this.cname = cname;
  26.     }
  27.     @Override
  28.     public String toString() {
  29.         return "Clazz{" +
  30.                 "cid=" + cid +
  31.                 ", cname='" + cname + '\'' +
  32.                 '}';
  33.     }
  34. }
复制代码
4.5.1.2、修改Student类

  1. package org.rain.spring.pojo;
  2. /**
  3. * @author liaojy
  4. * @date 2023/7/27 - 22:33
  5. */
  6. public class Student {
  7.     private Integer id;
  8.     private String name;
  9.     private Integer age;
  10.     private String sex;
  11.     private Clazz clazz;
  12.     public Student() {
  13.     }
  14.     public Student(Integer id, String name, Integer age, String sex, Clazz clazz) {
  15.         this.id = id;
  16.         this.name = name;
  17.         this.age = age;
  18.         this.sex = sex;
  19.         this.clazz = clazz;
  20.     }
  21.     public Clazz getClazz() {
  22.         return clazz;
  23.     }
  24.     public void setClazz(Clazz clazz) {
  25.         this.clazz = clazz;
  26.     }
  27.     public Integer getId() {
  28.         return id;
  29.     }
  30.     public void setId(Integer id) {
  31.         this.id = id;
  32.     }
  33.     public String getName() {
  34.         return name;
  35.     }
  36.     public void setName(String name) {
  37.         this.name = name;
  38.     }
  39.     public Integer getAge() {
  40.         return age;
  41.     }
  42.     public void setAge(Integer age) {
  43.         this.age = age;
  44.     }
  45.     public String getSex() {
  46.         return sex;
  47.     }
  48.     public void setSex(String sex) {
  49.         this.sex = sex;
  50.     }
  51.     @Override
  52.     public String toString() {
  53.         return "Student{" +
  54.                 "id=" + id +
  55.                 ", name='" + name + '\'' +
  56.                 ", age=" + age +
  57.                 ", sex='" + sex + '\'' +
  58.                 ", clazz=" + clazz +
  59.                 '}';
  60.     }
  61. }
复制代码
4.5.1.3、配置外部bean

  1.     <bean id="clazz" >
  2.         <property name="cid" value="1111"></property>
  3.         <property name="cname" value="三年E班"></property>
  4.     </bean>
复制代码
4.5.1.4、引用外部bean

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>            
复制代码
4.5.1.5、测试

  1.     @Test
  2.     public void testDIOuterBean(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentfour", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
4.5.2、方式二、级联

4.5.2.1、配置bean

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>   
  8.     <bean id="student" >
  9.         <property name="id" value="0011"></property>
  10.         <property name="name" value="张三"></property>
  11.         <property name="age" value="23"></property>
  12.         <property name="sex" value="男"></property>
  13.     </bean>        
复制代码
4.5.2.2、测试

  1.     @Test
  2.     public void testDICascade(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentFive", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
4.5.3、内部bean(常用)

4.5.3.1、配置bean

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>   
  8.     <bean id="student" >
  9.         <property name="id" value="0011"></property>
  10.         <property name="name" value="张三"></property>
  11.         <property name="age" value="23"></property>
  12.         <property name="sex" value="男"></property>
  13.     </bean>    <bean id="clazz" >
  14.         <property name="cid" value="1111"></property>
  15.         <property name="cname" value="三年E班"></property>
  16.     </bean>            
复制代码
4.5.3.2、测试

  1.     @Test
  2.     public void testDIInnerBean(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentSix", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
4.6、为数组类型的属性赋值

4.6.1、修改Student类


在Student类中添加或修改以下代码:
  1.     private String hobby[];
  2.     public String[] getHobby() {
  3.         return hobby;
  4.     }
  5.     public void setHobby(String[] hobby) {
  6.         this.hobby = hobby;
  7.     }
  8.     @Override
  9.     public String toString() {
  10.         return "Student{" +
  11.                 "id=" + id +
  12.                 ", name='" + name + '\'' +
  13.                 ", age=" + age +
  14.                 ", sex='" + sex + '\'' +
  15.                 ", clazz=" + clazz +
  16.                 ", hobby=" + Arrays.toString(hobby) +
  17.                 '}';
  18.     }
复制代码
4.6.2、配置bean

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>   
  8.     <bean id="student" >
  9.         <property name="id" value="0011"></property>
  10.         <property name="name" value="张三"></property>
  11.         <property name="age" value="23"></property>
  12.         <property name="sex" value="男"></property>
  13.     </bean>   
  14.     <bean id="student" >
  15.         <property name="id" value="0011"></property>
  16.         <property name="name" value="张三"></property>
  17.         <property name="age" value="23"></property>
  18.         <property name="sex" value="男"></property>
  19.     </bean>   
  20.     <bean id="student" >
  21.         <property name="id" value="0011"></property>
  22.         <property name="name" value="张三"></property>
  23.         <property name="age" value="23"></property>
  24.         <property name="sex" value="男"></property>
  25.     </bean>游泳                跑步                骑车    <bean id="clazz" >
  26.         <property name="cid" value="1111"></property>
  27.         <property name="cname" value="三年E班"></property>
  28.     </bean>
复制代码
4.6.3、测试

  1.     @Test
  2.     public void testDIArray(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentSeven", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
4.7、为List集合类型的属性赋值

4.7.1、修改Clazz类


在Clazzt类中添加或修改以下代码:
  1.     private List<Student> students;
  2.     public List<Student> getStudents() {
  3.         return students;
  4.     }
  5.     public void setStudents(List<Student> students) {
  6.         this.students = students;
  7.     }
  8.     @Override
  9.     public String toString() {
  10.         return "Clazz{" +
  11.                 "cid=" + cid +
  12.                 ", cname='" + cname + '\'' +
  13.                 ", students=" + students +
  14.                 '}';
  15.     }
复制代码
4.7.2、配置bean

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>    <bean id="clazz" >
  8.         <property name="cid" value="1111"></property>
  9.         <property name="cname" value="三年E班"></property>
  10.     </bean>    ]]>   
  11.     <bean id="student" >
  12.         <property name="id" value="0011"></property>
  13.         <property name="name" value="张三"></property>
  14.         <property name="age" value="23"></property>
  15.         <property name="sex" value="男"></property>
  16.     </bean>   
  17.     <bean id="student" >
  18.         <property name="id" value="0011"></property>
  19.         <property name="name" value="张三"></property>
  20.         <property name="age" value="23"></property>
  21.         <property name="sex" value="男"></property>
  22.     </bean>   
  23.     <bean id="student" >
  24.         <property name="id" value="0011"></property>
  25.         <property name="name" value="张三"></property>
  26.         <property name="age" value="23"></property>
  27.         <property name="sex" value="男"></property>
  28.     </bean>   
  29.     <bean id="student" >
  30.         <property name="id" value="0011"></property>
  31.         <property name="name" value="张三"></property>
  32.         <property name="age" value="23"></property>
  33.         <property name="sex" value="男"></property>
  34.     </bean>   
  35.     <bean id="student" >
  36.         <property name="id" value="0011"></property>
  37.         <property name="name" value="张三"></property>
  38.         <property name="age" value="23"></property>
  39.         <property name="sex" value="男"></property>
  40.     </bean>    <bean id="clazz" >
  41.         <property name="cid" value="1111"></property>
  42.         <property name="cname" value="三年E班"></property>
  43.     </bean>            
复制代码
4.7.3、测试

  1.     @Test
  2.     public void testDIList(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Clazz clazz = applicationContext.getBean("clazzTwo", Clazz.class);
  5.         System.out.println(clazz);
  6.     }
复制代码
4.8、为Map集合类型的属性赋值

4.8.1、创建教师类Teacher

  1. package org.rain.spring.pojo;
  2. /**
  3. * @author liaojy
  4. * @date 2023/7/30 - 12:10
  5. */
  6. public class Teacher {
  7.     private Integer tid;
  8.     private String tname;
  9.     public Teacher() {
  10.     }
  11.     public Teacher(Integer tid, String tname) {
  12.         this.tid = tid;
  13.         this.tname = tname;
  14.     }
  15.     public Integer getTid() {
  16.         return tid;
  17.     }
  18.     public void setTid(Integer tid) {
  19.         this.tid = tid;
  20.     }
  21.     public String getTname() {
  22.         return tname;
  23.     }
  24.     public void setTname(String tname) {
  25.         this.tname = tname;
  26.     }
  27.     @Override
  28.     public String toString() {
  29.         return "Teacher{" +
  30.                 "tid=" + tid +
  31.                 ", tname='" + tname + '\'' +
  32.                 '}';
  33.     }
  34. }
复制代码
4.8.2、修改Student类


在Student类中添加或修改以下代码:
  1.     private Map<String,Teacher> teacherMap;
  2.     public Map<String, Teacher> getTeacherMap() {
  3.         return teacherMap;
  4.     }
  5.     public void setTeacherMap(Map<String, Teacher> teacherMap) {
  6.         this.teacherMap = teacherMap;
  7.     }
  8.     @Override
  9.     public String toString() {
  10.         return "Student{" +
  11.                 "id=" + id +
  12.                 ", name='" + name + '\'' +
  13.                 ", age=" + age +
  14.                 ", sex='" + sex + '\'' +
  15.                 ", clazz=" + clazz +
  16.                 ", hobby=" + Arrays.toString(hobby) +
  17.                 ", teacherMap=" + teacherMap +
  18.                 '}';
  19.     }
复制代码
4.8.3、配置bean

  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>   
  8.     <bean id="student" >
  9.         <property name="id" value="0011"></property>
  10.         <property name="name" value="张三"></property>
  11.         <property name="age" value="23"></property>
  12.         <property name="sex" value="男"></property>
  13.     </bean>   
  14.     <bean id="student" >
  15.         <property name="id" value="0011"></property>
  16.         <property name="name" value="张三"></property>
  17.         <property name="age" value="23"></property>
  18.         <property name="sex" value="男"></property>
  19.     </bean>   
  20.     <bean id="student" >
  21.         <property name="id" value="0011"></property>
  22.         <property name="name" value="张三"></property>
  23.         <property name="age" value="23"></property>
  24.         <property name="sex" value="男"></property>
  25.     </bean>                游泳                跑步                骑车   
  26.     <bean id="student" >
  27.         <property name="id" value="0011"></property>
  28.         <property name="name" value="张三"></property>
  29.         <property name="age" value="23"></property>
  30.         <property name="sex" value="男"></property>
  31.     </bean>   
  32.     <bean id="student" >
  33.         <property name="id" value="0011"></property>
  34.         <property name="name" value="张三"></property>
  35.         <property name="age" value="23"></property>
  36.         <property name="sex" value="男"></property>
  37.     </bean>    <bean id="clazz" >
  38.         <property name="cid" value="1111"></property>
  39.         <property name="cname" value="三年E班"></property>
  40.     </bean>               
复制代码
4.8.4、测试

  1.     @Test
  2.     public void testDIMap(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentEight", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
4.9、为集合类型的属性赋值(解耦引用方式)

4.9.1、配置bean


注意:使用util:list、util:map标签必须引入相应的XML命名空间
  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>   
  8.     <bean id="student" >
  9.         <property name="id" value="0011"></property>
  10.         <property name="name" value="张三"></property>
  11.         <property name="age" value="23"></property>
  12.         <property name="sex" value="男"></property>
  13.     </bean>   
  14.     <bean id="student" >
  15.         <property name="id" value="0011"></property>
  16.         <property name="name" value="张三"></property>
  17.         <property name="age" value="23"></property>
  18.         <property name="sex" value="男"></property>
  19.     </bean>   
  20.     <bean id="student" >
  21.         <property name="id" value="0011"></property>
  22.         <property name="name" value="张三"></property>
  23.         <property name="age" value="23"></property>
  24.         <property name="sex" value="男"></property>
  25.     </bean>    <bean id="clazz" >
  26.         <property name="cid" value="1111"></property>
  27.         <property name="cname" value="三年E班"></property>
  28.     </bean>        游泳                跑步                骑车    <bean id="clazz" >
  29.         <property name="cid" value="1111"></property>
  30.         <property name="cname" value="三年E班"></property>
  31.     </bean>         
复制代码
4.9.2、测试

  1.     @Test
  2.     public void testDIUtil(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentNine", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
4.10、p命名空间(了解)

4.10.1、配置bean


注意:p命名空间的依赖注入,本质上是属性方式(setter方法)的依赖注入
  1.     <bean id="clazz" >
  2.         <property name="cid" value="1111"></property>
  3.         <property name="cname" value="三年E班"></property>
  4.     </bean>        
复制代码
4.10.2、测试

  1.     @Test
  2.     public void testDIP(){
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
  4.         Student student = applicationContext.getBean("studentTen", Student.class);
  5.         System.out.println(student);
  6.     }
复制代码
4.11、引入外部属性文件

4.11.1、引入数据库相关依赖

  1.     <bean id="clazz" >
  2.         <property name="cid" value="1111"></property>
  3.         <property name="cname" value="三年E班"></property>
  4.     </bean>    mysql            mysql-connector-java            5.1.49    <bean id="clazz" >
  5.         <property name="cid" value="1111"></property>
  6.         <property name="cname" value="三年E班"></property>
  7.     </bean>            com.alibaba            druid            1.0.31        
复制代码
4.11.2、创建外部属性文件

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=root
复制代码
4.11.3、配置bean


注意:使用context:property-placeholder标签必须引入相应的XML命名空间
  1.    
  2.     <bean id="student" >
  3.         <property name="id" value="0011"></property>
  4.         <property name="name" value="张三"></property>
  5.         <property name="age" value="23"></property>
  6.         <property name="sex" value="男"></property>
  7.     </bean>            
复制代码
4.11.4、测试

  1.     @Test
  2.     public void testDIOuterFile() throws SQLException {
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-datasource");
  4.         DruidDataSource datasource = applicationContext.getBean("datasource", DruidDataSource.class);
  5.         System.out.println(datasource.getConnection());
  6.     }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

卖不甜枣

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表