Spring從入門到源碼之IOC基本用法
1、spring_helloworld
使用maven的方式來構(gòu)建項目(Mavaen)
添加對應(yīng)的pom依賴
pom.xml
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.3.RELEASE</version> </dependency> </dependencies>
編寫代碼
Person.java
public class Person { private int id; private String name; private int age; private String gender; public int getId() { return id; } public void setId(int id) { this.id = id; public String getName() { return name; public void setName(String name) { this.name = name; public int getAge() { return age; public void setAge(int age) { this.age = age; public String getGender() { return gender; public void setGender(String gender) { this.gender = gender; @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; }
測試
MyTest.java
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml"); Person person = (Person) context.getBean("person"); System.out.println(person); } }
總結(jié):
一定要將配置文件添加到類路徑中,使用idea創(chuàng)建項目的時候要放在resource目錄下創(chuàng)建對象給屬性賦值的時候是通過setter方法實(shí)現(xiàn)的對象在IOC容器中存儲的時候都是單例的,如果需要多例需要修改屬性(默認(rèn)單例,可以改多例)對象在Spring容器創(chuàng)建完成的時候就已經(jīng)創(chuàng)建完成,不是需要用的時候才創(chuàng)建(Application創(chuàng)建時候?qū)ο缶蛣?chuàng)建了)ApplicationContext就是IOC容器的接口,可以通過此對象獲取容器中創(chuàng)建的對象
2、spring對象的獲取及屬性賦值方式
1、通過bean的id獲取IOC容器中的對象(上面已經(jīng)用過)
<?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"> <!-- id 對象唯一標(biāo)識 class 類名字:完全限定名字 --> <bean id = "person" class="com.yao.bean.Person"> <property name="id" value="001"/> <property name="name" value="yzh"/> <property name="age" value="12"/> <property name="gender" value="man"/> </bean> </beans>
- 一定要將配置文件添加到類路徑中,使用idea創(chuàng)建項目的時候要放在resource目錄下
- 創(chuàng)建對象給屬性賦值的時候是通過setter方法實(shí)現(xiàn)的
- 對象在IOC容器中存儲的時候都是單例的,如果需要多例需要修改屬性(默認(rèn)單例,可以改多例)
- 對象在Spring容器創(chuàng)建完成的時候就已經(jīng)創(chuàng)建完成,不是需要用的時候才創(chuàng)建(Application創(chuàng)建時候?qū)ο缶蛣?chuàng)建了)
- ApplicationContext就是IOC容器的接口,可以通過此對象獲取容器中創(chuàng)建的對象
2、通過bean的類型獲取對象
ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml"); Person bean = context.getBean(Person.class); System.out.println(bean);
注意:通過bean的類型在查找對象的時候,在配置文件中不能存在兩個類型一致的bean對象,如果有的話,可以通過如下方法
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml"); Person person = context.getBean("person", Person.class); System.out.println(person); } }
3、通過構(gòu)造器給bean對象賦值
總結(jié):
- constructor-arg 給person類添加構(gòu)造方法
- 使用構(gòu)造器賦值的時候可以省略name屬性,但是此時就要求必須嚴(yán)格按照構(gòu)造器參數(shù)的順序來填寫了
- 如果想不按照順序來添加參數(shù)值,那么可以搭配index屬性來使用
- 當(dāng)有多個參數(shù)個數(shù)相同,不同類型的構(gòu)造器的時候,可以通過type來強(qiáng)制類型
- 兩個重載的構(gòu)造方法,后面的構(gòu)造方法覆蓋前面的構(gòu)造方法。
constructor-arg中五個屬性值: value:賦值 name:成員屬性 ref :通過ref引用其他對象,引用外部bean index:如果想不按照順序來添加參數(shù)值,那么可以搭配index屬性來使用 type:當(dāng)有多個參數(shù)個數(shù)相同,不同類型的構(gòu)造器的時候,可以通過type來強(qiáng)制類型
ioc.xml
<!--給person類添加構(gòu)造方法--> <bean id="person2" class="com.mashibing.bean.Person"> <constructor-arg name="id" value="1"></constructor-arg> <constructor-arg name="name" value="lisi"></constructor-arg> <constructor-arg name="age" value="20"></constructor-arg> <constructor-arg name="gender" value="女"></constructor-arg> </bean> <!--在使用構(gòu)造器賦值的時候可以省略name屬性,但是此時就要求必須嚴(yán)格按照構(gòu)造器參數(shù)的順序來填寫了--> <bean id="person3" class="com.mashibing.bean.Person"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="lisi"></constructor-arg> <constructor-arg value="20"></constructor-arg> <constructor-arg value="女"></constructor-arg> </bean> <!--如果想不按照順序來添加參數(shù)值,那么可以搭配index屬性來使用--> <bean id="person4" class="com.mashibing.bean.Person"> <constructor-arg value="lisi" index="1"></constructor-arg> <constructor-arg value="1" index="0"></constructor-arg> <constructor-arg value="女" index="3"></constructor-arg> <constructor-arg value="20" index="2"></constructor-arg> </bean> <!--當(dāng)有多個參數(shù)個數(shù)相同,不同類型的構(gòu)造器的時候,可以通過type來強(qiáng)制類型--> 將person的age類型設(shè)置為Integer類型 public Person(int id, String name, Integer age) { this.id = id; this.name = name; this.age = age; System.out.println("Age"); } // 后面的構(gòu)造方法覆蓋前面的構(gòu)造方法。 public Person(int id, String name, String gender) { this.id = id; this.name = name; this.gender = gender; System.out.println("gender"); } <bean id="person5" class="com.mashibing.bean.Person"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="lisi"></constructor-arg> <constructor-arg value="20" type="java.lang.Integer"></constructor-arg> </bean> <!--如果不修改為integer類型,那么需要type跟index組合使用--> <bean id="person5" class="com.mashibing.bean.Person"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="lisi"></constructor-arg> <constructor-arg value="20" type="int" index="2"></constructor-arg> </bean>
4、通過命名空間為bean賦值,簡化配置文件中屬性聲明的寫法
p導(dǎo)入命名空間,還有c命名空間,更簡單的命名方式,僅此而已。、
1、導(dǎo)入命名空間--xml一些規(guī)范帶入就可以用啦
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
2、添加配置ioc.xml
<bean id="person6" class="com.mashibing.bean.Person" p:id="3" p:name="wangwu" p:age="22" p:gender="男"></bean>
5、為復(fù)雜類型進(jìn)行賦值操作(重點(diǎn))
總結(jié):
給各種復(fù)雜類型賦值,如集合、數(shù)組、其他對象等。
創(chuàng)建類如下:Person.java 、 Book.java、 Address.java 作為演示
Person.java
import java.util.*; public class Person { private int id; private String name="dahuang"; private int age; private String gender; private Address address; private String[] hobbies; private List<Book> books; private Set<Integer> sets; private Map<String,Object> maps; private Properties properties; // 沒用過這個數(shù)據(jù)類型 public Person(int id, String name, int age, String gender) { this.id = id; this.name = name; this.age = age; this.gender = gender; System.out.println("有參構(gòu)造器"); } public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; System.out.println("Age"); } public Person(int id, String name, String gender) { this.id = id; this.name = name; this.gender = gender; System.out.println("gender"); } public Person() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public String[] getHobbies() { return hobbies; } public void setHobbies(String[] hobbies) { this.hobbies = hobbies; } public Set<Integer> getSets() { return sets; } public void setSets(Set<Integer> sets) { this.sets = sets; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", address=" + address + ", hobbies=" + Arrays.toString(hobbies) + ", books=" + books + ", sets=" + sets + ", maps=" + maps + ", properties=" + properties + '}'; } }
Book.java
public class Book { private String name; private String author; private double price; public Book() { } public Book(String name, String author, double price) { this.name = name; this.author = author; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}'; } }
Address.java
public class Address { private String province; private String city; private String town; public Address() { } public Address(String province, String city, String town) { this.province = province; this.city = city; this.town = town; public String getProvince() { return province; public void setProvince(String province) { public String getCity() { return city; public void setCity(String city) { public String getTown() { return town; public void setTown(String town) { @Override public String toString() { return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + ", town='" + town + '\'' + '}'; }
ioc.xml
給復(fù)雜類型的賦值都在property標(biāo)簽內(nèi)進(jìn)行 1. 賦值為null <property name="name"> <null></null> </property> 2.外部引用 ref 3.引用內(nèi)部bean 4.為list賦值 5.為set賦值 <property name="sets"> <set> <value>111</value> <value>222</value> <value>222</value> </set> </property> 6。為map賦值 <property name="maps" ref="myMap"></property> <util:map id="myMap"> <entry key="key1" value="value1"></entry> <entry key="key2" value-ref="book2"></entry> <entry key="key03"> <bean class="com.yao.bean.Book"> <property name="name" value="西游記" ></property> <property name="author" value="吳承恩" ></property> <property name="price" value="100" ></property> </bean> </entry> </util:map> 7. 給數(shù)組賦值 <property name="hobbies"> <array> <value>book</value> <value>movie</value> <value>game</value> </array> </property>
ioc.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" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd" > <!--給復(fù)雜類型的賦值都在property標(biāo)簽內(nèi)進(jìn)行--> <bean id="person" class="com.mashibing.bean.Person"> <property name="name"> <!--賦空值--> <null></null> </property> <!--通過ref引用其他對象,引用外部bean--> <property name="address" ref="address"></property> <!--引用內(nèi)部bean--> <!-- <property name="address"> <bean class="com.mashibing.bean.Address"> <property name="province" value="北京"></property> <property name="city" value="北京"></property> <property name="town" value="西城區(qū)"></property> </bean> </property>--> <!--為list賦值--> <property name="books"> <list> <!--內(nèi)部bean--> <bean id="book1" class="com.mashibing.bean.Book"> <property name="name" value="多線程與高并發(fā)"></property> <property name="author" value="馬士兵"></property> <property name="price" value="1000"></property> </bean> <!--外部bean--> <ref bean="book2"></ref> </list> </property> <!--給map賦值--> <property name="maps" ref="myMap"></property> <!--給property賦值--> <property name="properties"> <props> <prop key="aaa">aaa</prop> <prop key="bbb">222</prop> </props> </property> <!--給數(shù)組賦值--> <property name="hobbies"> <array> <value>book</value> <value>movie</value> <value>game</value> </array> </property> <!--給set賦值--> <property name="sets"> <set> <value>111</value> <value>222</value> <value>222</value> </set> </property> </bean> <bean id="address" class="com.mashibing.bean.Address"> <property name="province" value="河北"></property> <property name="city" value="邯鄲"></property> <property name="town" value="武安"></property> </bean> <bean id="book2" class="com.mashibing.bean.Book"> <property name="name" value="JVM"></property> <property name="author" value="馬士兵"></property> <property name="price" value="1200"></property> </bean> <!--級聯(lián)屬性--> <bean id="person2" class="com.mashibing.bean.Person"> <property name="address" ref="address"></property> <property name="address.province" value="北京"></property> </bean> <!--util名稱空間創(chuàng)建集合類型的bean--> <util:map id="myMap"> <entry key="key1" value="value1"></entry> <entry key="key2" value-ref="book2"></entry> <entry key="key03"> <bean class="com.mashibing.bean.Book"> <property name="name" value="西游記" ></property> <property name="author" value="吳承恩" ></property> <property name="price" value="100" ></property> </bean> </entry> </util:map> </beans>
6、繼承關(guān)系bean的配置(暫時用不到)
意思就是bean可以繼承
parent:指定bean的配置信息繼承于哪個bean
parson2繼承parson 修改了name
abstract: 抽象bean,值在里面,還是有就是不能實(shí)例化。abstract="true"
自己演示一下,好像沒啥用奧。
ioc.xml
<bean id="person" class="com.mashibing.bean.Person"> <property name="id" value="1"></property> <property name="name" value="zhangsan"></property> <property name="age" value="21"></property> <property name="gender" value="男"></property> </bean> <!--parent:指定bean的配置信息繼承于哪個bean--> <bean id="person2" class="com.mashibing.bean.Person" parent="person"> <property name="name" value="lisi"></property> </bean>
7、bean對象創(chuàng)建的依賴關(guān)系(暫時用不到)
bean對象在創(chuàng)建的時候是按照bean在配置文件的順序決定的,也可以使用depend-on標(biāo)簽來決定順序
<bean id="book" class="com.mashibing.bean.Book" depends-on="person,address"></bean> <bean id="address" class="com.mashibing.bean.Address"></bean> <bean id="person" class="com.mashibing.bean.Person"></bean>
8、bean的作用域控制,是否是單例(一般兩種)
細(xì)節(jié)點(diǎn)區(qū)別:(面試的時候細(xì)節(jié)點(diǎn))
prototype:容器啟動的時候不會創(chuàng)建多實(shí)例bean,只有在獲取對象的時候才會創(chuàng)建該對象
singleton:在容器啟動完成之前就已經(jīng)創(chuàng)建好對象
四種作用域 singleton 單例(默認(rèn)的模式) prototype 多例模式 spring 4.x版本中: request: 每次發(fā)送請求都有一個新的對象 session:每次發(fā)送會話都有一個新的對象 這兩種方式幾乎不用。 測試singleton Person2 person2 = context.getBean("person2",Person.class); Person3 person3 = context.getBean("person3",Person.class); System.out.println(Person2== person3); // ture;
ioc.xml
<!-- bean的作用域:singleton、prototype、request、session 默認(rèn)情況下是單例的 prototype:多實(shí)例的 容器啟動的時候不會創(chuàng)建多實(shí)例bean,只有在獲取對象的時候才會創(chuàng)建該對象 每次創(chuàng)建都是一個新的對象 singleton:默認(rèn)的單例對象 在容器啟動完成之前就已經(jīng)創(chuàng)建好對象 獲取的所有對象都是同一個 --> <bean id="person4" class="com.mashibing.bean.Person" scope="prototype"></bean>
9、利用工廠模式創(chuàng)建bean對象
靜態(tài)工廠and實(shí)例工廠區(qū)別?
靜態(tài)工廠:工廠本身不需要創(chuàng)建對象,但是可以通過靜態(tài)方法調(diào)用,對象=工廠類.靜態(tài)工廠方法名();
實(shí)例工廠:工廠本身需要創(chuàng)建對象,工廠類 工廠對象=new 工廠類;工廠對象.get對象名();
優(yōu)缺點(diǎn):
實(shí)例工廠更容易擴(kuò)展。
PersonStaticFactory.java
public class PersonStaticFactory { // 靜態(tài)工廠類 public static Person getPerson(String name){ Person person = new Person(); person.setId(1); person.setName(name); return person; } }
ioc.xml
<!-- 靜態(tài)工廠的使用: class:指定靜態(tài)工廠類 factory-method:指定哪個方法是工廠方法 --> <bean id="person5" class="com.mashibing.factory.PersonStaticFactory" factory-method="getPerson"> <!--constructor-arg:可以為方法指定參數(shù)--> <constructor-arg value="lisi"></constructor-arg> </bean>
Test.java
工廠創(chuàng)建:用這個 不用PersonStaticFactory類,因?yàn)橛玫墓S方法創(chuàng)建的。 Person2 person2 = context.getBean("person2",Person.class); System.out.println(Person2);
PersonInstanceFactory.java
public class PersonInstanceFactory { // 實(shí)例工廠類 不加static??! public Person getPerson(String name){ Person person = new Person(); person.setId(1); person.setName(name); return person; } }
ioc.xml
<!--實(shí)例工廠使用--> <!--創(chuàng)建實(shí)例工廠類--> <bean id="personInstanceFactory" class="com.mashibing.factory.PersonInstanceFactory"></bean> <!-- factory-bean:指定使用哪個工廠實(shí)例 factory-method:指定使用哪個工廠實(shí)例的方法 --> <bean id="person6" class="com.mashibing.bean.Person" factory-bean="personInstanceFactory" factory-method="getPerson"> <constructor-arg value="wangwu"></constructor-arg> </bean>
10、繼承FactoryBean來創(chuàng)建對象(一般用不到,源碼中看的多)
BeanFactory 接口,規(guī)范
FactoryBean:獲取唯一的對象。
自己創(chuàng)建的對象也可交給spring管理。
意思就是可以把我自己創(chuàng)建的交給Spring管理使用如下>
使用:
1)實(shí)現(xiàn)FactoryBean<那個類對象這里是person>接口
2)三個方法:
工廠方法,返回需要創(chuàng)建的對象
返回對象的類型
否是單例對象
3)交給spring 配置ioc.xml下面有
總結(jié):
創(chuàng)建對象的補(bǔ)充,用的時候才創(chuàng)建不用不會創(chuàng)建。跟單例多例沒有關(guān)系
自己不用。源碼會用的多。
11、bean對象的初始化和銷毀方法(了解)
初始化跟單例和多例有區(qū)別的:(一般沒人使用,面試知道)
如果bean是單例,容器在啟動的時候會創(chuàng)建好,關(guān)閉的時候會銷毀創(chuàng)建的bean
如果bean是多例,獲取的時候創(chuàng)建對象,銷毀的時候不會有任何的調(diào)用
12、配置bean對象初始化方法的前后處理方法(了解)
POSTxxxx接口
13、spring創(chuàng)建第三方bean對象(配置數(shù)據(jù)庫連接池)
1、導(dǎo)入數(shù)據(jù)庫連接池的pom文件
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
2、編寫配置文件
<?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"> <!-- Spring管理第三方Bean對象 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="username" value="root"></property> <property name="password" value="123456"></property> <property name="url" value="jdbc:mysql://localhost:3306/demo"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean> </beans>
3、編寫測試文件
public class MyTest { public static void main(String[] args) throws SQLException { ApplicationContext context = new ClassPathXmlApplicationContext("ioc3.xml"); DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class); System.out.println(dataSource); System.out.println(dataSource.getConnection()); } }
4、spring引用外部配置文件
5、spring基于xml文件的自動裝配
前面有address這個類,在ioc.xml中,就是用的propotery裝配。
現(xiàn)在自動裝配:
怎么實(shí)現(xiàn)的?
byName:根據(jù)set方法后面首字幕大小寫裝配,必須首字幕小寫。
Type:按照類型裝配,有多個一樣的類型就報錯。
當(dāng)一個對象中需要引用另外一個對象的時候,在之前的配置中我們都是通過property標(biāo)簽來進(jìn)行手動配置的,其實(shí)在spring中還提供了一個非常強(qiáng)大的功能就是自動裝配,可以按照我們指定的規(guī)則進(jìn)行配置,配置的方式有以下幾種:
default/no:不自動裝配
byName:按照名字進(jìn)行裝配,以屬性名作為id去容器中查找組件,進(jìn)行賦值,如果找不到則裝配null
byType:按照類型進(jìn)行裝配,以屬性的類型作為查找依據(jù)去容器中找到這個組件,如果有多個類型相同的bean對象,那么會報異常,如果找不到則裝配null
constructor:按照構(gòu)造器進(jìn)行裝配,先按照有參構(gòu)造器參數(shù)的類型進(jìn)行裝配,沒有就直接裝配null;如果按照類型找到了多個,那么就使用參數(shù)名作為id繼續(xù)匹配,找到就裝配,找不到就裝配null
6、SpEL的使用(就是簡單語法糖,寫bean方便)
略:只有公司要求讓你用的時候你再去看,現(xiàn)在看了不用也沒啥用。
7、SpringIOC的注解應(yīng)用
Spring中注解標(biāo)識:
Spring中包含4個主要的組件添加注解: @Controller:控制器,推薦給controller層添加此注解(接受用戶請求 web用的到) @Service:業(yè)務(wù)邏輯,推薦給業(yè)務(wù)邏輯層添加此注解 @Repository:倉庫管理,推薦給數(shù)據(jù)訪問層添加此注解 @Component:給不屬于以上基層的**組件**添加此注解(啥也不懂就加這個)
注解使用步驟
使用注解需要如下步驟:
1、添加上述四個注解中的任意一個
2、添加自動掃描注解的組件,此操作需要依賴context命名空間
3、添加自動掃描的標(biāo)簽context:component-scan(當(dāng)前包下面的所有類都進(jìn)行掃描)
問題:在使用注解的時候咱們沒有定義id和class他是怎么識別的呢?
默認(rèn)類的名字首字母,小寫,但是可以起別名,一般沒人這么做,在注解標(biāo)識括號中添加value = "xxxxxx",如@Controler(value = "類的名字就可以改");
掃描包可以做耕細(xì)粒度的劃分(沒啥用)
使用@AutoWired進(jìn)行自動注入(理解困難,看看視頻)
面試題:
注意:當(dāng)使用AutoWired注解的時候,自動裝配的時候是根據(jù)類型實(shí)現(xiàn)的。
1、如果只找到一個,則直接進(jìn)行賦值,
2、如果沒有找到,則直接拋出異常,
3、如果找到多個,那么會按照變量名作為id繼續(xù)匹配,
1、匹配上直接進(jìn)行裝配
2、如果匹配不上則直接報異常還可以使用@Qualifier注解來指定id的名稱,讓spring不要使用變量名,當(dāng)使用@Qualifier注解的時候也會有兩種情況:
1、找到,則直接裝配
? 2、找不到,就會報錯
@AutoWired可以進(jìn)行定義在方法上
自動裝配的注解@AutoWired,@Resource
面試題:
在使用自動裝配的時候,出了可以使用@AutoWired注解之外,還可以使用@Resource注解,大家需要知道這**兩個注解的區(qū)別**。
? 1、@AutoWired:是spring中提供的注解,@Resource:是jdk中定義的注解,依靠的是java的標(biāo)準(zhǔn)
? 2、@AutoWired默認(rèn)是按照類型進(jìn)行裝配,默認(rèn)情況下要求依賴的對象必須存在,@Resource默認(rèn)是按照名字進(jìn)行匹配的,同時可以指定name屬性。
? 3、@AutoWired只適合spring框架,而@Resource擴(kuò)展性更好
6、泛型依賴注入(先跳過把)
到此這篇關(guān)于Spring從入門到源碼—IOC基本使用(二)的文章就介紹到這了,更多相關(guān)Spring入門IOC內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java簡單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過程解析
這篇文章主要介紹了Java簡單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12在Mac OS上安裝Java以及配置環(huán)境變量的基本方法
這篇文章主要介紹了在Mac OS上安裝Java以及配置環(huán)境變量的基本方法,包括查看所安裝Java版本的方法,需要的朋友可以參考下2015-10-10