您好,登錄后才能下訂單哦!
這篇文章主要介紹“Spring框架中控制反轉(zhuǎn)的介紹以及創(chuàng)建對(duì)象的方式”,在日常操作中,相信很多人在Spring框架中控制反轉(zhuǎn)的介紹以及創(chuàng)建對(duì)象的方式問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Spring框架中控制反轉(zhuǎn)的介紹以及創(chuàng)建對(duì)象的方式”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
控制反轉(zhuǎn)(Inversion on Control)在spring框架里面,一般交給Spring容器,這叫控制反轉(zhuǎn)
先來(lái)說(shuō)一下控制正轉(zhuǎn),
class Demo{ Student student = new Student(); }
簡(jiǎn)單地來(lái)說(shuō)就是自己去創(chuàng)建對(duì)象,需要什么對(duì)象,就創(chuàng)建什么對(duì)象,實(shí)在當(dāng)前文件中創(chuàng)建出來(lái)的,自己new出來(lái)的,這就叫做“控制正轉(zhuǎn)”
那么控制反轉(zhuǎn)是什么:
就是跟控制正轉(zhuǎn)反著來(lái),就是我需要的對(duì)象,我不需要自己new創(chuàng)建出來(lái),我只需要到一個(gè)地方去取過(guò)來(lái)用,相當(dāng)于讓別人創(chuàng)建出來(lái)我們需要的對(duì)象。另外,讓其它人來(lái)創(chuàng)建對(duì)象有兩種方式:第一種是直接調(diào)用有參構(gòu)造方法,另一種方法是調(diào)用構(gòu)造方法,然后使用set方法實(shí)現(xiàn)。
第一種方式是在spring的配置文件中(applicationContext.xml)中寫
applicationContext.xml
<!-- 其中scope是范圍的意思 singleton是單例模式,無(wú)論是否存在創(chuàng)建student這個(gè)操作,都會(huì)創(chuàng)建一個(gè)student對(duì)象,只創(chuàng)建一個(gè) prototype是多例模式,只要當(dāng)你進(jìn)行創(chuàng)建操作的時(shí)候才會(huì)進(jìn)行創(chuàng)建 舉個(gè)例子,單例模式就像一臺(tái)電腦,無(wú)論你用不用,它都在那里,也不會(huì)分裂多出一個(gè),也不會(huì)少一個(gè) 而多例模式就像擠牙膏,就是那種,你擠多少,出來(lái)多少,如果不擠就沒(méi)有 --> <bean scope="singleton" name="student" class="com.example.spring.entity.Student"> <constructor-arg name="id" value="1"/> <constructor-arg name="name" value="張三"/> <!-- 當(dāng)你創(chuàng)建的對(duì)像包括一個(gè)引用類型的時(shí)候,使用ref:reference:參考,引用來(lái)進(jìn)行構(gòu)造,調(diào)用的就是下面的course對(duì)象 --> <constructor-arg name="course" ref="com.example.spring.entity.Course"/> </bean>
Demo.class
public class Demo{ public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationCOntext("applicationContext.xml"); Student student = (Student) context.getBean("student"); // 根據(jù)你在applicationContext.xml的名字找到要?jiǎng)?chuàng)建的 } }
第二種方式是使用spring的配置文件中調(diào)用無(wú)參構(gòu)造方法,然后通過(guò)使用set方法將元素放進(jìn)去
applicationContext.xml
<bean scope="singleton" name="course" class="com.example.spring.entity.Course"> <property name="id" value="1"/> <property name="name" value="java"/> </bean>
該種方法是構(gòu)建一個(gè)無(wú)參構(gòu)造方法,然后將<property>
里面對(duì)應(yīng)的元素拿出來(lái),使用set方法放進(jìn)去,至于對(duì)應(yīng)的class文件也等用于是上面的Demo.class
對(duì)于第二種方法的優(yōu)化:
具體表現(xiàn)在三層架構(gòu)方面
StudentController.class
// 前面這個(gè)注釋等同于@Controller,在其他層次對(duì)應(yīng)的就是@service @Repository注釋是放在實(shí)現(xiàn)類上面的 // 相當(dāng)于 ApplicationContext.xml 文件中的 // <bean name="studentController" class="com.example.spring.controller"> // <property name="studentService" ref="studnetService"/> // </bean> @Controller(name="studentController") public class StudentController{ // @Resource(name="studnetService") 就相當(dāng)于調(diào)用setStudentService()方法將下面對(duì)應(yīng)的元素放進(jìn)Controller對(duì)象里 // <property name="studentService" ref="studnetService"/> // 然后 ref 在調(diào)用 // <bean name="studentService" class="com.example.spring.service.impl.StudentServiceImpl"><bean> @Resource(name="studentService") private StudentService studentService; public void selectAll(){ studentService.selectAll(); } public void setStudnetService(StudentService studnetService){ this.studnetService = studentService; } }
在次優(yōu)化變成
StudentController.class
@Controller public class StudnetController{ @Autowired // 這個(gè)會(huì)自動(dòng)進(jìn)行依賴注入,也不用特意寫一個(gè)set方法了很方便,但是要注意配置文件,要進(jìn)行配置,掃描注釋 private StudentService studentService; public void selectAll(){ studentService.selectAll(); } }
applicationContext.xml
<!-- 掃描base-package對(duì)應(yīng)的包下面類中所有的注解--> <context:component-scan base-package="com.example.spring"/>
問(wèn)題來(lái)了,Autowried是根據(jù)類型進(jìn)行注入的,但是如果某個(gè)接口存在多個(gè)實(shí)現(xiàn)的子類,那么Autowried是注入哪一個(gè)?又或者說(shuō)會(huì)報(bào)錯(cuò)?
答案:Error create bean with ‘studnetController',原因并不是因?yàn)镾tudentController里面,而是因?yàn)镾tudentController里面使用了Autowired進(jìn)行注入,而存在多個(gè)實(shí)現(xiàn)的幾口expected single matching bean but found 2: banjiServiceImpl,banjiServiceImpl2
,那么解決方案是:
@Controller public class StudnetController{ @Autowired // 添加下面的注解,寫明白是用那個(gè)注入 @Qualifier(value="studentServiceImpl2") private StudentService studentService; public void selectAll(){ studentService.selectAll(); } }
到此,關(guān)于“Spring框架中控制反轉(zhuǎn)的介紹以及創(chuàng)建對(duì)象的方式”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。