Spring bean为什么需要依赖注入
iamitnan · 116浏览 · 发布于2021-07-16 +关注

本篇文章主要介绍了Spring依赖注入的三种方式小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

目录

  • 具体步骤:

    • 样例1:

    • 样例2:


  • Spring单例模式和原型模式

    • 一、单例模式

    • 二、原型模式

    • 思考 为什么需要依赖注入

  • 总结

具体步骤:

1.创建一个maven项目 spring-day1-constructor

2.导入依赖

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!--这里是java 版本号-->

    <maven.compiler.source>11</maven.compiler.source>

    <maven.compiler.target>11</maven.compiler.target>

    <!--这里是方便版本控制-->

    <spring.version>5.3.1</spring.version>

    <lombok.version>1.18.20</lombok.version>

    <junit.version>4.12</junit.version>

</properties>

<dependencies>

    <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-beans</artifactId>

        <version>${spring.version}</version>

    </dependency>

    <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-context</artifactId>

        <version>${spring.version}</version>

    </dependency>

    <dependency>

        <groupId>org.projectlombok</groupId>

        <artifactId>lombok</artifactId>

        <version>${lombok.version}</version>

    </dependency>

    <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <version>${junit.version}</version>

    </dependency>

</dependencies>

3.工程项目结构

在这里插入图片描述

样例1:

1.创建一个Student类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

public class Student {

    private Long number;

    private String name;

    private String school;

    public void setNumber(Long number) {

        this.number = number;

    }

    public void setName(String name) {

        this.name = name;

    }

    public void setSchool(String school) {

        this.school = school;

    }

    public Student() {

    }

    public Student(Long number, String name, String school) {

        this.number = number;

        this.name = name;

        this.school = school;

    }

    @Override

    public String toString() {

        return "Student{" +

                "number=" + number +

                ", name='" + name + '\'' +

                ", school='" + school + '\'' +

                '}';

    }

}

写一个配置文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--这里是根据构造函数内的顺序往里面注入-->

    <bean id="s1" class="com.crush.pojo.Student">

       <constructor-arg index="0" value="12"/>

        <constructor-arg index="1" value="wyh"/>

        <constructor-arg index="2" value="北大"/>

    </bean>

    <!--这里是根据构造函数中的 类型来进行注入 -->

    <bean id="s2" class="com.crush.pojo.Student">

        <constructor-arg type="java.lang.Long" value="123"/>

        <constructor-arg type="java.lang.String" value="crush"/>

        <constructor-arg type="java.lang.String" value="浙江大学"/>

    </bean>

</beans>

3.测试

1

2

3

4

5

6

@org.junit.Test

 public void testStudent(){

     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

     Student student = applicationContext.getBean("s2", Student.class);

     System.out.println(student);

 }

样例2:

1.创建Teacher类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

public class Teacher {

    private String name;

    private String school;

    private List<Student> studentList;

    private Map<String,String> map;

    private Set<String> set;

    public Teacher(String name, String school, List<Student> studentList, Map<String, String> map, Set<String> set) {

        this.name = name;

        this.school = school;

        this.studentList = studentList;

        this.map = map;

        this.set = set;

    }

    @Override

    public String toString() {

        return "Teacher{" +

                "name='" + name + '\'' +

                ", school='" + school + '\'' +

                ", studentList=" + studentList +

                ", map=" + map +

                ", set=" + set +

                '}';

    }

}public class Teacher {

    private String name;

    private String school;

    private List<Student> studentList;

    private Map<String,String> map;

    private Set<String> set;

    public Teacher(String name, String school, List<Student> studentList, Map<String, String> map, Set<String> set) {

        this.name = name;

        this.school = school;

        this.studentList = studentList;

        this.map = map;

        this.set = set;

    }

    @Override

    public String toString() {

        return "Teacher{" +

                "name='" + name + '\'' +

                ", school='" + school + '\'' +

                ", studentList=" + studentList +

                ", map=" + map +

                ", set=" + set +

                '}';

    }

}

2.beans.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<bean id="teacher" class="com.crush.pojo.Teacher">

    <constructor-arg index="0" value="xxx"/>

    <constructor-arg index="1" value="北京大学"/>

    <constructor-arg index="2" >

        <list>

            <ref bean="s1"/>

            <ref bean="s2"/>

        </list>

    </constructor-arg>

    <constructor-arg index="3">

        <map>

            <entry key="k1" value="xiaowang"/>

        </map>

    </constructor-arg>

    <constructor-arg index="4">

        <set>

            <value>1</value>

            <value>2</value>

        </set>

    </constructor-arg>

</bean>

3.测试

1

2

3

4

5

6

@org.junit.Test

public void testTeacher(){

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

    Teacher teacher  = applicationContext.getBean("teacher", Teacher.class);

    System.out.println(teacher);

}

Spring单例模式和原型模式

一、单例模式

Spring默认是单例模式的。

以Student的那个样例1 为例。 scope=“singleton” 加上这么一个设置 当然默认也是它。

1

2

3

4

5

bean id="s1" class="com.crush.pojo.Student" scope="singleton">

    <constructor-arg index="0" value="12"/>

    <constructor-arg index="1" value="wyh"/>

    <constructor-arg index="2" value="北大"/>

</bean>

这个时候我们来进行测试

1

2

3

4

5

6

7

8

9

10

11

12

@org.junit.Test

public void testStudent(){

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

    Student student1 = applicationContext.getBean("s1", Student.class);

    Student student2 = applicationContext.getBean("s1", Student.class);

    // 并且如果我们对其中一个做了修改 ,其余也会跟着一起被修改

    // 可以看到我们只修改了一个

    student1.setSchool("梦中的学校");

    System.out.println(student1);

    System.out.println(student2);

    System.out.println(student1==student2);

}

在这里插入图片描述

二、原型模式

我们还是以**Student来做例子讲解 ** 注意:我们把原来设置改成了 scope=“prototype” 也就是原型模式

1

2

3

4

5

6

<!--这里是根据构造函数中的 类型来进行注入 -->

<bean id="s2" class="com.crush.pojo.Student" scope="prototype">

    <constructor-arg type="java.lang.Long" value="123"/>

    <constructor-arg type="java.lang.String" value="crush"/>

    <constructor-arg type="java.lang.String" value="浙江大学"/>

</bean>

接着测试

1

2

3

4

5

6

7

8

9

10

11

12

@org.junit.Test

public void testStudent(){

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

    Student student1 = applicationContext.getBean("s2", Student.class);

    Student student2 = applicationContext.getBean("s2", Student.class);

    // 并且如果我们对其中一个做了修改 ,其余也会跟着一起被修改

    // 可以看到我们只修改了一个

    student1.setSchool("梦中的学校");

    System.out.println(student1);

    System.out.println(student2);

    System.out.println(student1==student2);

}

在这里插入图片描述

思考 为什么需要依赖注入

为什么我们以前用一个对象 new一下就好了,但用了Spring 之后,反而还需要写

这样一段代码再去获取勒?明明感觉更麻烦啦丫?用这个又有什么样的好处呢?


1

2

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

Student student1 = applicationContext.getBean("s2", Student.class);


相关推荐

PHP实现部分字符隐藏

沙雕mars · 910浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 379浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 488浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 536浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 504浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
分类专栏
小鸟云服务器
扫码进入手机网页