您好,登錄后才能下訂單哦!
這篇文章主要介紹“Spring bean需要依賴注入的原因是什么”,在日常操作中,相信很多人在Spring bean需要依賴注入的原因是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”Spring bean需要依賴注入的原因是什么”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
具體步驟:
樣例1:
樣例2:
Spring單例模式和原型模式
一、單例模式
二、原型模式
思考 為什么需要依賴注入
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.創(chuàng)建一個(gè)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 + '\'' + '}'; } }
寫一個(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ù)中的 類型來進(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.測試
@org.junit.Test public void testStudent(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Student student = applicationContext.getBean("s2", Student.class); System.out.println(student); }
1.創(chuàng)建Teacher類
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.測試
@org.junit.Test public void testTeacher(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Teacher teacher = applicationContext.getBean("teacher", Teacher.class); System.out.println(teacher); }
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í)候我們來進(jìn)行測試
@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("夢中的學(xué)校"); System.out.println(student1); System.out.println(student2); System.out.println(student1==student2); }
我們還是以**Student來做例子講解 **注意:我們把原來設(shè)置改成了 scope=“prototype” 也就是原型模式
<!--這里是根據(jù)構(gòu)造函數(shù)中的 類型來進(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>
接著測試
@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("夢中的學(xué)校"); System.out.println(student1); System.out.println(student2); System.out.println(student1==student2); }
為什么我們以前用一個(gè)對(duì)象 new一下就好了,但用了Spring 之后,反而還需要寫
這樣一段代碼再去獲取勒?明明感覺更麻煩啦丫?用這個(gè)又有什么樣的好處呢?
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Student student1 = applicationContext.getBean("s2", Student.class);
到此,關(guān)于“Spring bean需要依賴注入的原因是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。