Spring IOC簡單理解及創(chuàng)建對象的方式
spring框架
控制反轉(Inversion on Control)在spring框架里面,一般交給Spring容器,這叫控制反轉
什么是控制反轉呢?
先來說一下控制正轉,
class Demo{
Student student = new Student();
}
簡單地來說就是自己去創(chuàng)建對象,需要什么對象,就創(chuàng)建什么對象,實在當前文件中創(chuàng)建出來的,自己new出來的,這就叫做“控制正轉”
那么控制反轉是什么:
就是跟控制正轉反著來,就是我需要的對象,我不需要自己new創(chuàng)建出來,我只需要到一個地方去取過來用,相當于讓別人創(chuàng)建出來我們需要的對象。另外,讓其它人來創(chuàng)建對象有兩種方式:第一種是直接調用有參構造方法,另一種方法是調用構造方法,然后使用set方法實現(xiàn)。
第一種方式是在spring的配置文件中(applicationContext.xml)中寫
applicationContext.xml
<!-- 其中scope是范圍的意思
singleton是單例模式,無論是否存在創(chuàng)建student這個操作,都會創(chuàng)建一個student對象,只創(chuàng)建一個
prototype是多例模式,只要當你進行創(chuàng)建操作的時候才會進行創(chuàng)建
舉個例子,單例模式就像一臺電腦,無論你用不用,它都在那里,也不會分裂多出一個,也不會少一個
而多例模式就像擠牙膏,就是那種,你擠多少,出來多少,如果不擠就沒有
-->
<bean scope="singleton" name="student" class="com.example.spring.entity.Student">
<constructor-arg name="id" value="1"/>
<constructor-arg name="name" value="張三"/>
<!-- 當你創(chuàng)建的對像包括一個引用類型的時候,使用ref:reference:參考,引用來進行構造,調用的就是下面的course對象 -->
<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的名字找到要創(chuàng)建的
}
}
第二種方式是使用spring的配置文件中調用無參構造方法,然后通過使用set方法將元素放進去
applicationContext.xml
<bean scope="singleton" name="course" class="com.example.spring.entity.Course">
<property name="id" value="1"/>
<property name="name" value="java"/>
</bean>
該種方法是構建一個無參構造方法,然后將<property>里面對應的元素拿出來,使用set方法放進去,至于對應的class文件也等用于是上面的Demo.class
對于第二種方法的優(yōu)化:
具體表現(xiàn)在三層架構方面
StudentController.class
// 前面這個注釋等同于@Controller,在其他層次對應的就是@service @Repository注釋是放在實現(xiàn)類上面的
// 相當于 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") 就相當于調用setStudentService()方法將下面對應的元素放進Controller對象里
// <property name="studentService" ref="studnetService"/>
// 然后 ref 在調用
// <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 // 這個會自動進行依賴注入,也不用特意寫一個set方法了很方便,但是要注意配置文件,要進行配置,掃描注釋
private StudentService studentService;
public void selectAll(){
studentService.selectAll();
}
}
applicationContext.xml
<!-- 掃描base-package對應的包下面類中所有的注解-->
<context:component-scan base-package="com.example.spring"/>
問題來了,Autowried是根據(jù)類型進行注入的,但是如果某個接口存在多個實現(xiàn)的子類,那么Autowried是注入哪一個?又或者說會報錯?
答案:Error create bean with ‘studnetController',原因并不是因為StudentController里面,而是因為StudentController里面使用了Autowired進行注入,而存在多個實現(xiàn)的幾口expected single matching bean but found 2: banjiServiceImpl,banjiServiceImpl2,那么解決方案是:
@Controller
public class StudnetController{
@Autowired
// 添加下面的注解,寫明白是用那個注入
@Qualifier(value="studentServiceImpl2")
private StudentService studentService;
public void selectAll(){
studentService.selectAll();
}
}
到此這篇關于Spring IOC簡單理解的文章就介紹到這了,更多相關Spring IOC內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計
這篇文章主要為大家詳細介紹了java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
使用Sharding-JDBC對數(shù)據(jù)進行分片處理詳解
這篇文章主要介紹了使用Sharding-JDBC對數(shù)據(jù)進行分片處理詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java連接MySQL數(shù)據(jù)庫增刪改查的通用方法(推薦)
下面小編就為大家?guī)硪黄狫ava連接MySQL數(shù)據(jù)庫增刪改查的通用方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
spring根據(jù)controller中接收請求參數(shù)不同走不同service的實現(xiàn)方法
這篇文章主要給大家介紹了關于spring實現(xiàn)根據(jù)controller中接收請求參數(shù)不同走不同service的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2018-11-11

