Spring bean為什么需要依賴注入
具體步驟:
1.創(chuàng)建一個(gè)maven項(xiàng)目 spring-day1-constructor
2.導(dǎo)入依賴
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--這里是java 版本號(hào)--> <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.工程項(xiàng)目結(jié)構(gòu)
樣例1:
1.創(chuàng)建一個(gè)Student類(lèi)
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 + '\'' + '}'; } }
寫(xiě)一個(gè)配置文件
<?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"> <!--這里是根據(jù)構(gòu)造函數(shù)內(nèi)的順序往里面注入--> <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> <!--這里是根據(jù)構(gòu)造函數(shù)中的 類(lèi)型來(lái)進(jìn)行注入 --> <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="浙江大學(xué)"/> </bean> </beans>
3.測(cè)試
@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.創(chuàng)建Teacher類(lèi)
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
<bean id="teacher" class="com.crush.pojo.Teacher"> <constructor-arg index="0" value="xxx"/> <constructor-arg index="1" value="北京大學(xué)"/> <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.測(cè)試
@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默認(rèn)是單例模式的。
以Student的那個(gè)樣例1 為例。 scope=“singleton” 加上這么一個(gè)設(shè)置 當(dāng)然默認(rèn)也是它。
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>
這個(gè)時(shí)候我們來(lái)進(jìn)行測(cè)試
@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); // 并且如果我們對(duì)其中一個(gè)做了修改 ,其余也會(huì)跟著一起被修改 // 可以看到我們只修改了一個(gè) student1.setSchool("夢(mèng)中的學(xué)校"); System.out.println(student1); System.out.println(student2); System.out.println(student1==student2); }
二、原型模式
我們還是以**Student來(lái)做例子講解 ** 注意:我們把原來(lái)設(shè)置改成了 scope=“prototype” 也就是原型模式
<!--這里是根據(jù)構(gòu)造函數(shù)中的 類(lèi)型來(lái)進(jìn)行注入 --> <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="浙江大學(xué)"/> </bean>
接著測(cè)試
@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); // 并且如果我們對(duì)其中一個(gè)做了修改 ,其余也會(huì)跟著一起被修改 // 可以看到我們只修改了一個(gè) student1.setSchool("夢(mèng)中的學(xué)校"); System.out.println(student1); System.out.println(student2); System.out.println(student1==student2); }
思考 為什么需要依賴注入
為什么我們以前用一個(gè)對(duì)象 new一下就好了,但用了Spring 之后,反而還需要寫(xiě)
這樣一段代碼再去獲取勒?明明感覺(jué)更麻煩啦丫?用這個(gè)又有什么樣的好處呢?
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Student student1 = applicationContext.getBean("s2", Student.class);
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
淺談MyBatisPlus中LocalDateTime引發(fā)的一些問(wèn)題和解決辦法
MyBatisPlus進(jìn)行數(shù)據(jù)庫(kù)操作時(shí),我們經(jīng)常會(huì)遇到處理日期時(shí)間類(lèi)型的需求,本文主要介紹了淺談MyBatisPlus中LocalDateTime引發(fā)的一些問(wèn)題和解決辦法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07最有價(jià)值的50道java面試題 適用于準(zhǔn)入職Java程序員
這篇文章主要為大家分享了最有價(jià)值的50道java面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對(duì)hashCode方法的設(shè)計(jì)、垃圾收集的堆和代進(jìn)行剖析,感興趣的小伙伴們可以參考一下2016-05-05Spring實(shí)戰(zhàn)之抽象Bean和子Bean定義與用法示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之抽象Bean和子Bean定義與用法,結(jié)合實(shí)例形式分析了Spring抽象Bean和子Bean相關(guān)配置、定義與使用操作技巧,需要的朋友可以參考下2019-11-11關(guān)于bigDecimal類(lèi)的精度保留方法
這篇文章主要介紹了關(guān)于bigDecimal類(lèi)的精度保留方法,計(jì)算機(jī)存儲(chǔ)的浮點(diǎn)數(shù)受存儲(chǔ)bit位數(shù)影響,只能保證一定范圍內(nèi)精準(zhǔn),超過(guò)bit范圍的只能取近似值,Java使用java.math.BigDecimal專門(mén)處理小數(shù)精度,需要的朋友可以參考下2023-07-07SpringBoot3集成Thymeleaf的過(guò)程詳解
在現(xiàn)代的Web開(kāi)發(fā)中,構(gòu)建靈活、動(dòng)態(tài)的用戶界面是至關(guān)重要的,Spring Boot和Thymeleaf的結(jié)合為開(kāi)發(fā)者提供了一種簡(jiǎn)單而強(qiáng)大的方式來(lái)創(chuàng)建動(dòng)態(tài)的Web應(yīng)用,本文將介紹如何在Spring Boot項(xiàng)目中集成Thymeleaf,并展示一些基本的使用方法,需要的朋友可以參考下2024-01-01