Spring從入門(mén)到源碼之IOC基本用法
1、spring_helloworld
使用maven的方式來(lái)構(gòu)建項(xiàng)目(Mavaen)
添加對(duì)應(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>編寫(xiě)代碼
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 + '\'' +
'}';
}測(cè)試
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é):
一定要將配置文件添加到類(lèi)路徑中,使用idea創(chuàng)建項(xiàng)目的時(shí)候要放在resource目錄下創(chuàng)建對(duì)象給屬性賦值的時(shí)候是通過(guò)setter方法實(shí)現(xiàn)的對(duì)象在IOC容器中存儲(chǔ)的時(shí)候都是單例的,如果需要多例需要修改屬性(默認(rèn)單例,可以改多例)對(duì)象在Spring容器創(chuàng)建完成的時(shí)候就已經(jīng)創(chuàng)建完成,不是需要用的時(shí)候才創(chuàng)建(Application創(chuàng)建時(shí)候?qū)ο缶蛣?chuàng)建了)ApplicationContext就是IOC容器的接口,可以通過(guò)此對(duì)象獲取容器中創(chuàng)建的對(duì)象
2、spring對(duì)象的獲取及屬性賦值方式
1、通過(guò)bean的id獲取IOC容器中的對(duì)象(上面已經(jīng)用過(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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
id 對(duì)象唯一標(biāo)識(shí)
class 類(lèi)名字:完全限定名字
-->
<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>- 一定要將配置文件添加到類(lèi)路徑中,使用idea創(chuàng)建項(xiàng)目的時(shí)候要放在resource目錄下
- 創(chuàng)建對(duì)象給屬性賦值的時(shí)候是通過(guò)setter方法實(shí)現(xiàn)的
- 對(duì)象在IOC容器中存儲(chǔ)的時(shí)候都是單例的,如果需要多例需要修改屬性(默認(rèn)單例,可以改多例)
- 對(duì)象在Spring容器創(chuàng)建完成的時(shí)候就已經(jīng)創(chuàng)建完成,不是需要用的時(shí)候才創(chuàng)建(Application創(chuàng)建時(shí)候?qū)ο缶蛣?chuàng)建了)
- ApplicationContext就是IOC容器的接口,可以通過(guò)此對(duì)象獲取容器中創(chuàng)建的對(duì)象
2、通過(guò)bean的類(lèi)型獲取對(duì)象
ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
Person bean = context.getBean(Person.class);
System.out.println(bean);注意:通過(guò)bean的類(lèi)型在查找對(duì)象的時(shí)候,在配置文件中不能存在兩個(gè)類(lèi)型一致的bean對(duì)象,如果有的話,可以通過(guò)如下方法
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、通過(guò)構(gòu)造器給bean對(duì)象賦值
總結(jié):
- constructor-arg 給person類(lèi)添加構(gòu)造方法
- 使用構(gòu)造器賦值的時(shí)候可以省略name屬性,但是此時(shí)就要求必須嚴(yán)格按照構(gòu)造器參數(shù)的順序來(lái)填寫(xiě)了
- 如果想不按照順序來(lái)添加參數(shù)值,那么可以搭配index屬性來(lái)使用
- 當(dāng)有多個(gè)參數(shù)個(gè)數(shù)相同,不同類(lèi)型的構(gòu)造器的時(shí)候,可以通過(guò)type來(lái)強(qiáng)制類(lèi)型
- 兩個(gè)重載的構(gòu)造方法,后面的構(gòu)造方法覆蓋前面的構(gòu)造方法。
constructor-arg中五個(gè)屬性值: value:賦值 name:成員屬性 ref :通過(guò)ref引用其他對(duì)象,引用外部bean index:如果想不按照順序來(lái)添加參數(shù)值,那么可以搭配index屬性來(lái)使用 type:當(dāng)有多個(gè)參數(shù)個(gè)數(shù)相同,不同類(lèi)型的構(gòu)造器的時(shí)候,可以通過(guò)type來(lái)強(qiáng)制類(lèi)型
ioc.xml
<!--給person類(lèi)添加構(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)造器賦值的時(shí)候可以省略name屬性,但是此時(shí)就要求必須嚴(yán)格按照構(gòu)造器參數(shù)的順序來(lái)填寫(xiě)了-->
<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>
<!--如果想不按照順序來(lái)添加參數(shù)值,那么可以搭配index屬性來(lái)使用-->
<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)有多個(gè)參數(shù)個(gè)數(shù)相同,不同類(lèi)型的構(gòu)造器的時(shí)候,可以通過(guò)type來(lái)強(qiáng)制類(lèi)型-->
將person的age類(lèi)型設(shè)置為Integer類(lèi)型
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類(lèi)型,那么需要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、通過(guò)命名空間為bean賦值,簡(jiǎn)化配置文件中屬性聲明的寫(xiě)法
p導(dǎo)入命名空間,還有c命名空間,更簡(jiǎn)單的命名方式,僅此而已。、
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ù)雜類(lèi)型進(jìn)行賦值操作(重點(diǎn))
總結(jié):
給各種復(fù)雜類(lèi)型賦值,如集合、數(shù)組、其他對(duì)象等。
創(chuàng)建類(lèi)如下: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; // 沒(méi)用過(guò)這個(gè)數(shù)據(jù)類(lèi)型
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ù)雜類(lèi)型的賦值都在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ù)雜類(lèi)型的賦值都在property標(biāo)簽內(nèi)進(jìn)行-->
<bean id="person" class="com.mashibing.bean.Person">
<property name="name">
<!--賦空值-->
<null></null>
</property>
<!--通過(guò)ref引用其他對(duì)象,引用外部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>
<!--級(jí)聯(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)建集合類(lèi)型的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的配置(暫時(shí)用不到)
意思就是bean可以繼承
parent:指定bean的配置信息繼承于哪個(gè)bean
parson2繼承parson 修改了name
abstract: 抽象bean,值在里面,還是有就是不能實(shí)例化。abstract="true"
自己演示一下,好像沒(méi)啥用奧。
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的配置信息繼承于哪個(gè)bean-->
<bean id="person2" class="com.mashibing.bean.Person" parent="person">
<property name="name" value="lisi"></property>
</bean>7、bean對(duì)象創(chuàng)建的依賴關(guān)系(暫時(shí)用不到)
bean對(duì)象在創(chuàng)建的時(shí)候是按照bean在配置文件的順序決定的,也可以使用depend-on標(biāo)簽來(lái)決定順序
<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ū)別:(面試的時(shí)候細(xì)節(jié)點(diǎn))
prototype:容器啟動(dòng)的時(shí)候不會(huì)創(chuàng)建多實(shí)例bean,只有在獲取對(duì)象的時(shí)候才會(huì)創(chuàng)建該對(duì)象
singleton:在容器啟動(dòng)完成之前就已經(jīng)創(chuàng)建好對(duì)象
四種作用域
singleton 單例(默認(rèn)的模式)
prototype 多例模式
spring 4.x版本中:
request: 每次發(fā)送請(qǐng)求都有一個(gè)新的對(duì)象
session:每次發(fā)送會(huì)話都有一個(gè)新的對(duì)象
這兩種方式幾乎不用。
測(cè)試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í)例的
容器啟動(dòng)的時(shí)候不會(huì)創(chuàng)建多實(shí)例bean,只有在獲取對(duì)象的時(shí)候才會(huì)創(chuàng)建該對(duì)象
每次創(chuàng)建都是一個(gè)新的對(duì)象
singleton:默認(rèn)的單例對(duì)象
在容器啟動(dòng)完成之前就已經(jīng)創(chuàng)建好對(duì)象
獲取的所有對(duì)象都是同一個(gè)
-->
<bean id="person4" class="com.mashibing.bean.Person" scope="prototype"></bean>9、利用工廠模式創(chuàng)建bean對(duì)象
靜態(tài)工廠and實(shí)例工廠區(qū)別?
靜態(tài)工廠:工廠本身不需要?jiǎng)?chuàng)建對(duì)象,但是可以通過(guò)靜態(tài)方法調(diào)用,對(duì)象=工廠類(lèi).靜態(tài)工廠方法名();
實(shí)例工廠:工廠本身需要?jiǎng)?chuàng)建對(duì)象,工廠類(lèi) 工廠對(duì)象=new 工廠類(lèi);工廠對(duì)象.get對(duì)象名();
優(yōu)缺點(diǎn):
實(shí)例工廠更容易擴(kuò)展。
PersonStaticFactory.java
public class PersonStaticFactory { // 靜態(tài)工廠類(lè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)工廠類(lèi)
factory-method:指定哪個(gè)方法是工廠方法
-->
<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)建:用這個(gè) 不用PersonStaticFactory類(lèi),因?yàn)橛玫墓S方法創(chuàng)建的。
Person2 person2 = context.getBean("person2",Person.class);
System.out.println(Person2);PersonInstanceFactory.java
public class PersonInstanceFactory { // 實(shí)例工廠類(lèi) 不加static?。?
public Person getPerson(String name){
Person person = new Person();
person.setId(1);
person.setName(name);
return person;
}
}ioc.xml
<!--實(shí)例工廠使用-->
<!--創(chuàng)建實(shí)例工廠類(lèi)-->
<bean id="personInstanceFactory" class="com.mashibing.factory.PersonInstanceFactory"></bean>
<!--
factory-bean:指定使用哪個(gè)工廠實(shí)例
factory-method:指定使用哪個(gè)工廠實(shí)例的方法
-->
<bean id="person6" class="com.mashibing.bean.Person" factory-bean="personInstanceFactory" factory-method="getPerson">
<constructor-arg value="wangwu"></constructor-arg>
</bean>10、繼承FactoryBean來(lái)創(chuàng)建對(duì)象(一般用不到,源碼中看的多)
BeanFactory 接口,規(guī)范
FactoryBean:獲取唯一的對(duì)象。
自己創(chuàng)建的對(duì)象也可交給spring管理。
意思就是可以把我自己創(chuàng)建的交給Spring管理使用如下>
使用:
1)實(shí)現(xiàn)FactoryBean<那個(gè)類(lèi)對(duì)象這里是person>接口
2)三個(gè)方法:
工廠方法,返回需要?jiǎng)?chuàng)建的對(duì)象
返回對(duì)象的類(lèi)型
否是單例對(duì)象
3)交給spring 配置ioc.xml下面有
總結(jié):
創(chuàng)建對(duì)象的補(bǔ)充,用的時(shí)候才創(chuàng)建不用不會(huì)創(chuàng)建。跟單例多例沒(méi)有關(guān)系
自己不用。源碼會(huì)用的多。
11、bean對(duì)象的初始化和銷(xiāo)毀方法(了解)
初始化跟單例和多例有區(qū)別的:(一般沒(méi)人使用,面試知道)
如果bean是單例,容器在啟動(dòng)的時(shí)候會(huì)創(chuàng)建好,關(guān)閉的時(shí)候會(huì)銷(xiāo)毀創(chuàng)建的bean
如果bean是多例,獲取的時(shí)候創(chuàng)建對(duì)象,銷(xiāo)毀的時(shí)候不會(huì)有任何的調(diào)用
12、配置bean對(duì)象初始化方法的前后處理方法(了解)
POSTxxxx接口
13、spring創(chuàng)建第三方bean對(duì)象(配置數(shù)據(jù)庫(kù)連接池)
1、導(dǎo)入數(shù)據(jù)庫(kù)連接池的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、編寫(xiě)配置文件
<?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對(duì)象
-->
<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、編寫(xiě)測(cè)試文件
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文件的自動(dòng)裝配
前面有address這個(gè)類(lèi),在ioc.xml中,就是用的propotery裝配。
現(xiàn)在自動(dòng)裝配:
怎么實(shí)現(xiàn)的?
byName:根據(jù)set方法后面首字幕大小寫(xiě)裝配,必須首字幕小寫(xiě)。
Type:按照類(lèi)型裝配,有多個(gè)一樣的類(lèi)型就報(bào)錯(cuò)。
當(dāng)一個(gè)對(duì)象中需要引用另外一個(gè)對(duì)象的時(shí)候,在之前的配置中我們都是通過(guò)property標(biāo)簽來(lái)進(jìn)行手動(dòng)配置的,其實(shí)在spring中還提供了一個(gè)非常強(qiáng)大的功能就是自動(dòng)裝配,可以按照我們指定的規(guī)則進(jìn)行配置,配置的方式有以下幾種:
default/no:不自動(dòng)裝配
byName:按照名字進(jìn)行裝配,以屬性名作為id去容器中查找組件,進(jìn)行賦值,如果找不到則裝配null
byType:按照類(lèi)型進(jìn)行裝配,以屬性的類(lèi)型作為查找依據(jù)去容器中找到這個(gè)組件,如果有多個(gè)類(lèi)型相同的bean對(duì)象,那么會(huì)報(bào)異常,如果找不到則裝配null
constructor:按照構(gòu)造器進(jìn)行裝配,先按照有參構(gòu)造器參數(shù)的類(lèi)型進(jìn)行裝配,沒(méi)有就直接裝配null;如果按照類(lèi)型找到了多個(gè),那么就使用參數(shù)名作為id繼續(xù)匹配,找到就裝配,找不到就裝配null
6、SpEL的使用(就是簡(jiǎn)單語(yǔ)法糖,寫(xiě)bean方便)
略:只有公司要求讓你用的時(shí)候你再去看,現(xiàn)在看了不用也沒(méi)啥用。
7、SpringIOC的注解應(yīng)用
Spring中注解標(biāo)識(shí):
Spring中包含4個(gè)主要的組件添加注解: @Controller:控制器,推薦給controller層添加此注解(接受用戶請(qǐng)求 web用的到) @Service:業(yè)務(wù)邏輯,推薦給業(yè)務(wù)邏輯層添加此注解 @Repository:倉(cāng)庫(kù)管理,推薦給數(shù)據(jù)訪問(wèn)層添加此注解 @Component:給不屬于以上基層的**組件**添加此注解(啥也不懂就加這個(gè))
注解使用步驟
使用注解需要如下步驟:
1、添加上述四個(gè)注解中的任意一個(gè)
2、添加自動(dòng)掃描注解的組件,此操作需要依賴context命名空間
3、添加自動(dòng)掃描的標(biāo)簽context:component-scan(當(dāng)前包下面的所有類(lèi)都進(jìn)行掃描)
問(wèn)題:在使用注解的時(shí)候咱們沒(méi)有定義id和class他是怎么識(shí)別的呢?
默認(rèn)類(lèi)的名字首字母,小寫(xiě),但是可以起別名,一般沒(méi)人這么做,在注解標(biāo)識(shí)括號(hào)中添加value = "xxxxxx",如@Controler(value = "類(lèi)的名字就可以改");
掃描包可以做耕細(xì)粒度的劃分(沒(méi)啥用)
使用@AutoWired進(jìn)行自動(dòng)注入(理解困難,看看視頻)
面試題:
注意:當(dāng)使用AutoWired注解的時(shí)候,自動(dòng)裝配的時(shí)候是根據(jù)類(lèi)型實(shí)現(xiàn)的。
1、如果只找到一個(gè),則直接進(jìn)行賦值,
2、如果沒(méi)有找到,則直接拋出異常,
3、如果找到多個(gè),那么會(huì)按照變量名作為id繼續(xù)匹配,
1、匹配上直接進(jìn)行裝配
2、如果匹配不上則直接報(bào)異常還可以使用@Qualifier注解來(lái)指定id的名稱,讓spring不要使用變量名,當(dāng)使用@Qualifier注解的時(shí)候也會(huì)有兩種情況:
1、找到,則直接裝配
? 2、找不到,就會(huì)報(bào)錯(cuò)
@AutoWired可以進(jìn)行定義在方法上
自動(dòng)裝配的注解@AutoWired,@Resource
面試題:
在使用自動(dòng)裝配的時(shí)候,出了可以使用@AutoWired注解之外,還可以使用@Resource注解,大家需要知道這**兩個(gè)注解的區(qū)別**。
? 1、@AutoWired:是spring中提供的注解,@Resource:是jdk中定義的注解,依靠的是java的標(biāo)準(zhǔn)
? 2、@AutoWired默認(rèn)是按照類(lèi)型進(jìn)行裝配,默認(rèn)情況下要求依賴的對(duì)象必須存在,@Resource默認(rèn)是按照名字進(jìn)行匹配的,同時(shí)可以指定name屬性。
? 3、@AutoWired只適合spring框架,而@Resource擴(kuò)展性更好
6、泛型依賴注入(先跳過(guò)把)
到此這篇關(guān)于Spring從入門(mén)到源碼—IOC基本使用(二)的文章就介紹到這了,更多相關(guān)Spring入門(mén)IOC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java簡(jiǎn)單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Java簡(jiǎn)單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
jpa 使用@Column來(lái)定義字段類(lèi)型
這篇文章主要介紹了jpa使用@Column來(lái)定義字段類(lèi)型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring Boot獲取微信用戶信息的超簡(jiǎn)單方法
這篇文章主要給大家介紹了關(guān)于Spring Boot獲取微信用戶信息的超簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
在Mac OS上安裝Java以及配置環(huán)境變量的基本方法
這篇文章主要介紹了在Mac OS上安裝Java以及配置環(huán)境變量的基本方法,包括查看所安裝Java版本的方法,需要的朋友可以參考下2015-10-10

