成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring:bean注入--Set方法注入

瀏覽:46日期:2023-07-02 09:24:00
目錄Set 方法注入總結(jié)Set 方法注入

1.新建一個(gè)空的 maven項(xiàng)目。

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)

Spring:bean注入--Set方法注入

4.新建包 com.crush.pojo

5.新建Java類Student

@Data // set、get 方法@AllArgsConstructor // 全參構(gòu)造@NoArgsConstructor // 無(wú)參構(gòu)造public class Student { /** * 學(xué)號(hào) */ private Long number; /** * 學(xué)生姓名 */ private String name; /** * 所在學(xué)校 */ private String school;}

resource 下 beans.xml文件

<?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'> <!--第一種方式 set 方式注入1、有set方法才可以注入2、默認(rèn)是單例模式 singleton--> <bean scope='singleton'><!--值可以跟在在標(biāo)簽后面 也可以 寫在標(biāo)簽內(nèi)部--><property name='number'> <value>1</value></property><property name='name' value='wyh'/><property name='school' value='hngy'/> </bean><!--這個(gè)id 就是 applicationContext.getBean('【bean-id】', Student.class);此處id 大多數(shù)時(shí)候命名規(guī)則就是 類名的第一個(gè)字母改為小寫class:Studentbean id一般就為: student --> <bean scope='singleton'><!--值可以跟在在標(biāo)簽后面 也可以 寫在標(biāo)簽內(nèi)部--><property name='number'> <value>1</value></property><property name='name' value='wyh'/><property name='school' value='hngy'/> </bean></beans>

寫一個(gè)測(cè)試類

public class Test { /** * 通過(guò) ClassPathXmlApplicationContext 獲取 Spring 應(yīng)用程序的 上下文 ApplicationContext */ @org.junit.Test public void test(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext('beans.xml');// 第一種方式 獲取ioc 容器中的Student 強(qiáng)制類型轉(zhuǎn)換Student student = (Student) applicationContext.getBean('student');// 第二種方式 直接在后面寫明類的標(biāo)簽。Student student1 = applicationContext.getBean('student', Student.class);// student.setName('cccc'); 給其中一個(gè)修改 就會(huì)全部修改 可以自己打開(kāi)測(cè)試下 System.out.println(student);System.out.println(student1);// 這里結(jié)果為true // 解釋:因?yàn)镾pring 默認(rèn)構(gòu)造出來(lái)的對(duì)象 默認(rèn)是單例的。 無(wú)論獲取多少次 ,都是單例的。System.out.println(student==student1); }/** * 通過(guò) FileSystemXmlApplicationContext 獲取 Spring 應(yīng)用程序的 上下文 ApplicationContext * 還有第三種是 通過(guò)Web服務(wù)器實(shí)例化 ApplicationContext 容器 */ @org.junit.Test public void test2(){//這里的路徑 也可以 用絕對(duì)路徑ApplicationContext applicationContext = new FileSystemXmlApplicationContext('srcmainresourcesbeans.xml');Student student = applicationContext.getBean('student', Student.class);System.out.println(student); }}

小小思考

為什么 new ClassPathXmlApplicationContext(“beans.xml”); 要用ApplicationContext 來(lái)接收,而不用ClassPathXmlApplicationContext 接收呢?

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

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext('beans.xml');

解釋:

按照面向接口編程的思想,聲明變量應(yīng)該是接口類型的,然后創(chuàng)建一個(gè)該接口的實(shí)現(xiàn)類的實(shí)例賦值給該變量。 ApplicationContext是接口,ClassPathXmlApplicationContext是它的一個(gè)實(shí)現(xiàn)類。所以你就看到了 ApplicationContext ac = new ClassPathXmlApplicationContext(…)

總結(jié)

本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注好吧啦網(wǎng)的更多內(nèi)容!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产亚洲精品久久久久久午夜 | 深夜福利视频大全在线观看 | 国产精品a人片在线观看 | 欧美一区二区三区久久综 | 空姐毛片| 国产精品免费视频一区 | 美女图片131亚洲午夜 | 四色永久 | 国产成人精品视频免费大全 | 久久久久久久久中文字幕 | 99热久久国产综合精品久久国产 | 欧美在线视频免费 | 国产精品国产三级国产专区5o | 91精品国产欧美一区二区 | 国产一区二区三区高清视频 | 久久国产精彩视频 | 成人国产精品一区二区网站 | 欧美中文字幕一区 | 日本久久伊人 | 亚洲精品www久久久久久久软件 | 天堂一区二区在线观看 | 欧美成人激情在线 | 欧美成人免费一区在线播放 | 草草影院第一页yycccom | 国内精品久久久久影院亚洲 | 毛片网站在线播放 | 欧美日韩久久 | 99在线精品视频免费观里 | 夜色精品国产一区二区 | 成人免费视频软件网站 | 亚洲第一页在线视频 | 在线中文字幕视频 | 成人看的一级毛片 | 一二三区在线观看 | 久久久久久福利 | 深夜爽爽爽福利动态图 | 国产成人国产在线观看入口 | 亚洲国产欧美精品一区二区三区 | 亚洲精品色综合色在线观看 | 91精品国产高清久久久久久91 | 中国日本高清免费视频网 |