【spring學習-day2】IOC-DI-scope-setter和構造器注入
【補充】 這是早就寫了的文章,如今有新的理解,想補充完成。
1.scope
2.IOC
3.DI
4.setter注入和構造器注入
5.init和destory
【打頭說明】 IOC說的是控制反轉,意思就是讓spring來建立物件,竟然讓spring來建立物件,總得告訴spring誰需要它來建立。
- Spring中提供的其他IoC標籤 1,版型標籤:就是代表不同的樣子;四種標籤也能起到和component相同的作用 1,@Controller:用於控制器(Action/如果在SpringMVC中,@Controller有特殊意義); 2,@Service:用於服務物件; 3,@Repository:用於DAO; 4,@Component:用於其他元件; 注意,不同版型標籤達到的目的都是一樣的,都是讓spring去掃描這些類; 這些標籤對於Spring沒有區別,是寫給程式猿看的;
DI是依賴注入,那麼怎麼注入?用下面的標籤 DI相關的註解: 1,在Spring中,有兩個註解可以用來做DI:@Autowired, @Resource
----------------------------------------------------原來寫的------------------------------------------------------------
spring官網的文件說明https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-reference/core.html#beans-introduction
Spring IOC : 又稱控制反轉,控制什麼,反轉什麼呢。 人話解釋:大部分的業務實現都需要2個物件或者2個物件以上合作完成,那麼在用到另一個物件的時候,傳統就需要new Object();如下程式碼
classA
{
AInterface a;
A(){}
AMethod()//一個方法
{
a = new AInterfaceImp();
}
}
這樣一來,耦合關係就產生了; 因此,控制反轉說的是,把建立物件的權利交給spring,這就是所謂的控制反轉。(待會用程式碼說明)
Spring DI :又叫依賴注入,是一個工程。官方給出2種注入方式,一種是建構函式注入,一種是setter注入。
方式一:setter注入
public class College {
public void getCollegeName(){
System.out.println("------Java吃雞學院!------");
}
}
public class School {
String name;
String address;
College college;
public String getName() { return name;}
public void setName(String name) { this.name = name; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public College getCollege() { return college; }
public void setCollege(College college) { this.college = college; }
}
<bean class="com.ecp.flproject.test.College" id="college"></bean>
<bean class="com.ecp.flproject.test.School" id="school">
<property name="college" ref="college"/>
<property name="name" value="Java學院"/>
<property name="address" value="中國"/>
</bean>
測試程式碼(物件的建立必須經過建構函式)
方式二:構造器注入
由於構造器的引數可能順序不一樣,或者型別不一樣,這種情況下如何注入? 可以通過index,type,name進行指定。
public class SchoolDome1 {
String name;
String address;
College college;
public SchoolDome1(String name,String address,College college){
this.name = name;
this.address = address;
this.college = college;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public College getCollege() { return college; }
public void setCollege(College college) { this.college = college; }
}
<bean class="com.ecp.flproject.test.College" id="college"></bean>
<bean class="com.ecp.flproject.test.SchoolDome1" id="schoolDome1">
<constructor-arg index="0" value="Java大學"></constructor-arg>
<constructor-arg index="1" value="中國"></constructor-arg>
<constructor-arg index="2" ref="college"></constructor-arg>
</bean>
測試程式碼
1.我看原始碼ApplicationContext 是繼承了 BeanFactory。 2.ConfigurableApplicationContext 繼承了 ApplicationContext。 3.官方說該介面org.springframework.context.ApplicationContext表示Spring IoC容器,並負責例項化,配置和組裝上述bean。 4.官方還說ApplicationContext包含了所有功能BeanFactory。
補充說明:
【scope】 1.singleton 單例的,只會在容器中建立一次。(預設的) 2.prototype 多例的,每次去獲取的時候,都是新的物件。
【init和destory】 bean上有2個屬性 init-method = “init”,物件建立之後馬上執行這個初始化方法。 destory-method = “destory”,容器關閉後執行銷燬方法。
可以在注入之後,再執行一些方法,但前提是,要提前在類裡寫好這個方法;
Spring中在不同容器下,bean的初始化時機;
1,BeanFactory是在真正去拿bean的時候,才會去建立bean的例項(延遲例項化); 2,ApplicationContext是在容器啟動的時候就已經建立好了bean的例項(迫切例項化);
延遲例項化的優勢:應用啟動的時候佔用的資源很小;適合資源緊張的應用;
迫切例項化的優勢:應用在啟動的時候已經例項化所有的物件,就可以在啟動的時候發現物件的錯誤或者為這些物件做更多的事情(比如AOP);
也可以在bean上面新增lazy-init=true來讓ApplicationContext也延遲例項化物件;還可以在beans上面配置default-lazy-init=true讓整個beans中所有的bean全部延遲例項化;