Spring是如何通過(guò)IOC來(lái)創(chuàng)建對(duì)象的?

一、IOC如何獲取對(duì)象
1.1 Spring是如何獲取對(duì)象的?
①新建一個(gè)maven項(xiàng)目后導(dǎo)入webmvc的依賴:因?yàn)閣ebmvc包含了很多其他依賴,為了省事,干脆導(dǎo)入一個(gè)總的,方便省事!版本嘛!個(gè)人比較喜歡用最新版。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
復(fù)制代碼②新建實(shí)體測(cè)試類:
public class Person {
private String name;
private int age;
private String like;
private String high;
//get、set、tostring方法為了篇幅省略,可以自己加或者使用lombok
}
復(fù)制代碼③在resources目錄下新建ContextAplication.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">
<bean id="Person" class="entity.Person">
<property name="age" value="23"></property>
<property name="name" value="丁大大"></property>
<property name="like" value="釣魚"></property>
<property name="high" value="173"></property>
</bean>
</beans>
復(fù)制代碼④以上前提之后,你會(huì)發(fā)現(xiàn)你的測(cè)試Person類種發(fā)生了變化:點(diǎn)擊可以跳轉(zhuǎn)到指定的xml位置哦~
⑤測(cè)試:
Context.getBean() 不指定類時(shí),需要強(qiáng)制轉(zhuǎn)換,所以建議使用第二種方式來(lái)獲取對(duì)象
public class Test {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
// Person person = (Person) Context.getBean("Person");//這里不指定的話需要強(qiáng)轉(zhuǎn),建議用下面的方式來(lái)拿對(duì)象
Person person = Context.getBean("Person",Person.class);
System.out.println(person);
}
}
復(fù)制代碼⑥執(zhí)行結(jié)果如下:成功拿到值!
⑦總結(jié):
控制: 傳統(tǒng)的程序?qū)ο蟮膭?chuàng)建是由程序來(lái)控制創(chuàng)建的。
反轉(zhuǎn): 交給Spring容器來(lái)創(chuàng)建對(duì)象,而程序只負(fù)責(zé)被動(dòng)的接收對(duì)象。這就是反轉(zhuǎn)。
依賴注入: 就是通過(guò)set方法來(lái)注入的。
1.2 改造案例由xml選擇創(chuàng)建對(duì)象
①xml:
<bean id="StudentMapperImpl" class="mapper.impl.StudentMapperImpl"/>
<bean id="TeacherMapperImpl" class="mapper.impl.TeacherMapperImpl"/>
<bean id="PersonServiceImpl" class="service.impl.PersonServiceImpl">
<property name="studentMapper" ref="StudentMapperImpl"/>
</bean>
復(fù)制代碼②測(cè)試:
ApplicationContext Context1 = new ClassPathXmlApplicationContext("ContextAplication.xml");
PersonServiceImpl personServiceImpl = Context1.getBean("PersonServiceImpl", PersonServiceImpl.class);
personServiceImpl.getPersonInfo();
復(fù)制代碼③執(zhí)行結(jié)果:
⑤總結(jié):
對(duì)象由Spring 來(lái)創(chuàng)建 , 管理 , 裝配 !這就是 IOC!
二、IOC是通過(guò)什么方式來(lái)創(chuàng)建對(duì)象的?
2.1 通過(guò)無(wú)參構(gòu)造函數(shù)來(lái)創(chuàng)建對(duì)象
①以Person類為例子,但是加上一個(gè)無(wú)參構(gòu)造函數(shù)!
public class Person {
private String name;
private int age;
private String like;
private String high;
public Person() {
//輸出一句話證明自己被調(diào)用了!
System.out.println("我是Person類的無(wú)參構(gòu)造函數(shù)!我被調(diào)用了?。。。?);
}
//set、get、tostring方法因?yàn)槠蚴÷?,?qǐng)手動(dòng)加上!
}
復(fù)制代碼②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">
<bean id="Person" class="entity.Person">
<property name="age" value="23"></property>
<property name="name" value="丁大大"></property>
<property name="like" value="釣魚"></property>
<property name="high" value="173"></property>
</bean>
</beans>
復(fù)制代碼③測(cè)試類:
public class Test {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
Person person = Context.getBean("Person", Person.class);
System.out.println(person);
}
}
復(fù)制代碼④執(zhí)行結(jié)果:
⑤去除無(wú)參構(gòu)造,增加有參構(gòu)造:
xml配置程序直接報(bào)錯(cuò):
⑥總結(jié):
Spring創(chuàng)建對(duì)象默認(rèn)是通過(guò)無(wú)參構(gòu)造函數(shù)創(chuàng)建的!
能通過(guò)有參構(gòu)造函數(shù)來(lái)創(chuàng)建對(duì)象嘛?能!看下面!
2.2 通過(guò)有參構(gòu)造方法來(lái)創(chuàng)建對(duì)象
①前提于 2.1 一致,新增有參構(gòu)造函數(shù):(因?yàn)轭愔校J(rèn)的也就是不寫構(gòu)造參數(shù)就是無(wú)參構(gòu)造,寫了有參構(gòu)造才能真正意義上去除無(wú)參構(gòu)造,這個(gè)不用解釋太多吧,java基礎(chǔ)的內(nèi)容了~?。?/p>
public Person(String name, int age, String like, String high) {
this.name = name;
this.age = age;
this.like = like;
this.high = high;
}
復(fù)制代碼②xml配置文件中要發(fā)生一定的改變:
<?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">
<bean id="Person" class="entity.Person">
<!-- <property name="name" value="丁大大"></property>-->
<!-- <property name="age" value="23"></property>-->
<!-- <property name="like" value="釣魚"></property>-->
<!-- <property name="high" value="173"></property>-->
<constructor-arg index="0" value="丁大大"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value="釣魚"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
復(fù)制代碼③執(zhí)行結(jié)果:
⑤總結(jié):
無(wú)參構(gòu)造函數(shù)指定值時(shí)使用 propert 標(biāo)簽
有參構(gòu)造函數(shù)指定值時(shí)使用 constructor-arg 標(biāo)簽,三種寫法
基本類型可以直接寫
引用類型得加上全稱,如:java.lang.String
位置跟index差不多,依次從上到下對(duì)應(yīng)屬性的從上到下。
index --通過(guò)下標(biāo)來(lái)給屬性賦值
name --通過(guò)屬性名稱來(lái)給屬性賦值
type -- 指定屬性的類型來(lái)給屬性賦值
在配置文件加載的時(shí)候。其中管理的對(duì)象都已經(jīng)初始化了!
三、Spring的配置
3.1 alias(別名):
為bean設(shè)置別名,可設(shè)置多個(gè)!
①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">
<alias name="Person" alias="personAlias1"/>
<alias name="Person" alias="personAlias2"/>
<alias name="Person" alias="personAlias3"/>
<bean id="Person" class="entity.Person">
<constructor-arg index="0" value="丁大大"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value="釣魚"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
復(fù)制代碼②測(cè)試類:
public class Test {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
Person person = Context.getBean("personAlias1", Person.class);
System.out.println(person);
}
}
復(fù)制代碼③執(zhí)行結(jié)果:
④總結(jié):講實(shí)話,這玩意用處不大,因?yàn)檫€有更好的方式來(lái)設(shè)置別名!
3.2 Bean的配置:
bean就相當(dāng)于java對(duì)象,由Spring創(chuàng)建和管理
①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">
<alias name="Person" alias="personAlias1"/>
<alias name="Person" alias="personAlias2"/>
<alias name="Person" alias="personAlias3"/>
<bean id="Person" name="person1,person2 person3;person4" class="entity.Person">
<constructor-arg index="0" value="丁大大"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value="釣魚"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
復(fù)制代碼②測(cè)試類:
public class Test {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
Person person = Context.getBean("person4", Person.class);
System.out.println(person);
}
}
復(fù)制代碼③執(zhí)行結(jié)果:
④總結(jié):
id是bean的唯一標(biāo)識(shí)符
如果沒有配置id,那么name相當(dāng)于標(biāo)識(shí)符,并且可以設(shè)置多個(gè)
name也是別名,可多個(gè),并且可以通過(guò) 逗號(hào) 空格 分號(hào) 來(lái)分隔,是不是比alias別名方便?所以設(shè)置別名我們一般使用name
id和name同時(shí)存在,name只是別名,不是標(biāo)識(shí)符
class是類的全限定名 包名+類名

3.3 import(團(tuán)隊(duì)合作之導(dǎo)入)
①在實(shí)際工作的開發(fā)過(guò)程中,一個(gè)項(xiàng)目可能由多個(gè)程序員來(lái)進(jìn)行開發(fā),所以為了解決共性問題,比如:同一文件提交時(shí)都進(jìn)行了修改可能引起沖突,所以我們使用import來(lái)解耦!
②新建多個(gè)xml配置文件:
ContextAplication.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">
<import resource="dyj1.xml"/>
<import resource="dyj3.xml"/>
<import resource="dyj2.xml"/>
</beans>
復(fù)制代碼dyj1.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">
<bean name="person1,person2 person3;person4" class="entity.Person">
<constructor-arg index="0" value="丁大大1"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value="釣魚1"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
復(fù)制代碼dyj2.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">
<bean name="person1,person2 person3;person4" class="entity.Person">
<constructor-arg index="0" value="丁大大2"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value="釣魚2"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
復(fù)制代碼dyj3.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">
<bean name="person1,person2 person3;person4" class="entity.Person">
<constructor-arg index="0" value="丁大大3"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value="釣魚3"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
復(fù)制代碼③執(zhí)行:
④總結(jié):
如果三個(gè)文件都是對(duì)同一個(gè)操作同一個(gè)類,或者說(shuō)內(nèi)容一致,那么就以主xml中從上到下最后一個(gè)impot為準(zhǔn)。
語(yǔ)法格式:
<import resource="{path}/beans.xml"/>優(yōu)點(diǎn):
每個(gè)人開發(fā)的都是獨(dú)立的,如果重復(fù)的內(nèi)容,Spring會(huì)幫我們自動(dòng)合并!
降低了程序的沖突性!
大大提高了后期代碼的可維護(hù)性!
作者:大魚丶
鏈接:https://juejin.cn/post/6972781931167481893
來(lái)源:掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
