Spring bean为什么需要依赖注入

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

目录

具体步骤:

样例1:

样例2:

Spring单例模式和原型模式

一、单例模式

二、原型模式

思考 为什么需要依赖注入

总结

具体步骤:

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

2.导入依赖

UTF-811115.3.11.18.204.12org.springframeworkspring-beans${spring.version}org.springframeworkspring-context${spring.version}org.projectlomboklombok${lombok.version}junitjunit${junit.version}

3.工程项目结构

样例1:

1.创建一个Student类

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 + ''' + '}'; } }

一个配置文件

3.测试

@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类

public class Teacher { private String name; private String school; private List studentList; private Map map; private Set set; public Teacher(String name, String school, List studentList, Map map, Set 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 studentList; private Map map; private Set set; public Teacher(String name, String school, List studentList, Map map, Set 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

12

3.测试

@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” 加上这么一个设置 当然认也是它。

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

这个时候我们来进行测试

@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” 也就是原型模式

接着测试

@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 之后,反而还需要写

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

ApplicationContext applicationContext = new ClasspathXmlApplicationContext("beans.xml"); Student student1 = applicationContext.getBean("s2", Student.class);

总结

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...