Spring bean为什么需要依赖注入

网友投稿 258 2022-10-11


Spring bean为什么需要依赖注入

目录具体步骤:样例1:样例2:Spring单例模式和原型模式一、单例模式二、原型模式思考 为什么需要依赖注入总结

具体步骤:

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

2.导入依赖

UTF-8

11

11

5.3.1

1.18.20

4.12

org.springframework

spring-beans

${spring.version}

org.springframework

spring-context

${spring.version}

&ltgokDcFwai;dependency>

org.projectlombok

lombok

${lombok.version}

junit

junit

${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 + '\'' +

'}';

}

}

写一个配置文件

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

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

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

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

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

1

2

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);

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注我们的更多内容!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:汉源高科百兆2光3电4电光纤级联型光电转换器工业矿用本安型光纤收发器迷你嵌入式主板
下一篇:矿用百兆2光4电光纤收发器本安型光纤交换机嵌入式安装支持5-12伏供电
相关文章

 发表评论

暂时没有评论,来抢沙发吧~