1 spring_helloworld
1.1 使用手動加載jar包的方式實現
- 現在基本不使用這種方式
- 導包:其中第一個jar包在maven官網找到,剩下幾個在spring壓縮包下載地址中,下載的spring-5.2.6.RELEASE-dist.zip文件解壓即可得到
- commons-logging-1.2.jar
- spring-beans-5.2.3.RELEASE.jar
- spring-context-5.2.3.RELEASE.jar
- spring-core-5.2.3.RELEASE.jar
- spring-expression-5.2.3.RELEASE.jar
- Person
package com.mashibing.bean;
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 + '\'' +
'}';
}
}
- 編寫spring的配置文件
- src–New–XML Configuration File-- Spring Config–ioc.xml
- 導jar包後纔有這個選項
<?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標籤就表示一個對象
id:這個對象的唯一標識,不能有兩個id相同的bean
class:註冊對象的完全限定名
-->
<bean id="person" class="com.mashibing.bean.Person">
<!--使用property標籤給對象的屬性賦值
name:表示屬性的名稱
value:表示屬性的值
-->
<property name="id" value="1"></property>
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
<property name="gender" value="男"></property>
</bean>
</beans>
- SpringDemoTest:測試類
package com.mashibing.test;
import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//容器中的對象在使用該對象前,就已經把對象創建好了
public class SpringDemoTest {
public static void main(String[] args) {
//ApplicationContext:表示ioc容器
//ClassPathXmlApplicationContext:表示從當前classpath路徑中獲取xml文件的配置
//根據spring的配置文件來獲取ioc容器對象
ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
Person person = (Person) context.getBean("person");
//獲取對象時不需要強制類型轉換的方法
//Person person = context.getBean("person",Person.class);
System.out.println(person);
}
}
1.2 使用maven的方式來構建項目
- 先下載老師提供的3.5.4的maven,如果一個jar需要依賴其他jar,maven會幫我們自動下載
- 有些jar包在國外,下載非常慢,所以需要修改maven倉庫默認地址,在配置文件conf\settings.xml中
<!--可以配置下載到本地的哪個路徑下-->
<localRepository>E:\javaMvn</localRepository>
...
<!--用阿里雲的維護的一個jar包倉庫,下載速度有保證-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
- 配置環境變量
- MAVEN_HOME:E:\Program Files (x86)\apache-maven-3.5.4
- Path:%MAVEN_HOME%\bin
- cmd輸入mvn -v,能輸出版本號,說明配置無誤
- 在project中配置maven
- idea中創建maven項目
- GroupId:com.mashibing,類似原來包名命名規則
- ArtifactId:spring_study,類似原來project命名規則
- 在右下角出現maven project need to imported時,選enable auto import
- 修改pom依賴:
- 在maven官網搜索Spring Context,複製其xml配置粘貼到pom.xml中的dependencies標籤中,由於Context包在core模塊內,此時Core模塊下所有需要的jar包都會被自動引入
- pom.xml
- 在maven官網搜索Spring Context,複製其xml配置粘貼到pom.xml中的dependencies標籤中,由於Context包在core模塊內,此時Core模塊下所有需要的jar包都會被自動引入
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_01</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
</project>
- 編寫spring的配置文件
- resources–New–XML Configuration File-- Spring Config–ioc.xml
- 使用maven構建項目時,resources中內容會自動加載到項目的classes文件夾下,也就是在classpath中
- 剩下內容與1.1中一致
1.3 總結
- 以上兩種方式創建spring的項目都是可以的,但是在現在的企業開發環境中使用更多的還是maven這樣的方式,無須自己處理jar之間的依賴關係,也無須提前下載jar包,只需要配置相關的pom即可,因此推薦大家使用maven的方式,具體的maven操作大家可以看maven的詳細操作文檔
- 搭建spring項目需要注意的點
- 一定要將配置文件添加到類路徑中,使用idea的maven創建項目的時候要放在resource目錄下
- 使用導包的方式時,別忘了commons-logging-1.2.jar包
- 細節點
- ApplicationContext就是IOC容器的接口,可以通過此對象獲取容器中創建的對象
- 其實現類有FileSystemXmlApplicationContext和ClassPathXmlApplicationContext。前者爲從文件系統中查找配置文件,後者爲從classpath中查找文件。一般不用前者,因爲如果使用前者windows開發的內容布在linux上時,還需要改路徑
- 對象在Spring容器創建完成的時候就已經創建完成,不是需要用的時候才創建
- 對象在IOC容器中存儲的時候都是單例的,如果需要多例需要修改屬性
- 創建對象給屬性賦值的時候是通過setter方法實現的。也就是對象必須有getter和setter方法,否則配置文件都會報錯,也無法注入
- 對象的屬性是由setter/getter方法決定的,而不是定義的成員屬性。也就是setter方法叫什麼,配置文件中,property應該寫什麼,而不是屬性本身叫什麼決定
2 獲取對象
2.1 通過bean的id
上面已經使用過
2.2 通過bean的類型
- MyTest
import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
Person bean = context.getBean(Person.class);
System.out.println(bean);
}
}
- 注意:通過bean的類型在查找對象的時候,在配置文件中不能存在兩個類型一致的bean對象,如果有的話,可以通過如下方法
Person person = context.getBean("person", Person.class);
3 爲對象屬性賦值
3.1 通過setter方法
- 上面已經使用過
- 使用這種方法,可以想象spring一定是先創建出一個對象,再調用該對象的setter方法爲其屬性賦值
- 因此使用這種方法前提是對象必須有無參構造方法,因爲spring底層實現是反射,沒有無參構造器會報錯
3.2 通過構造器
- name:參數列表中形參名
<!--給person類添加構造方法,參數name由構造方法中形參列表名決定-->
<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>
- value:爲形參傳的實參值
- 如果形參爲Date類型,那麼value要固定寫爲"2020/02/08"這種格式
<!--在使用構造器賦值的時候可以省略name屬性,但是此時就要求必須嚴格按照構造器參數的順序來填寫了,而且如果有多個參數個數相同,不同類型的構造器的時候,默認調用最後面的構造方法-->
<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>
- index:值的下標,從0開始
<!--如果想不按照順序來添加參數值,那麼可以搭配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>
- type:值的類型
<!--如果形參中爲int型,那麼type也寫int型,如果形參中爲Integer,形參中應該寫java.lang.Integer-->
<!--需要type跟index組合使用,才能使用第一個構造器,必須指定第幾個參數是哪個構造器,如果只指定type="int",還是使用第二個構造器,而value對應的20,會被當做int類型的id處理-->
<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>
public Person(int id, String name, Integer 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");
}
- 日常工作中,都使用name+value方式,很少有人用index或type的方式,但要注意各種情況出現的問題
3.3 通過命名空間:簡化配置文件中屬性聲明的寫法
- 導入命名空間xmlns:p:類似JSTL標籤的用法,其實就是導入了另一種寫法
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns:xml name spcae,xsi:xml schema instance,schemaLocation表示記錄了定義規範的位置,ioc.xml中能寫bean、constructor-arg等這些標籤,就是因爲schemaLocation中定義了xsd的位置-->
<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">
- 具體配置
<!--p:表示使用命名空間p-->
<bean id="person6" class="com.mashibing.bean.Person" p:id="3" p:name="wangwu" p:age="22" p:gender="男"></bean>
3.4 爲複雜類型進行賦值操作
- 在之前的測試代碼中,我們都是給最基本的屬性進行賦值操作,在正常的企業級開發中還會遇到給各種複雜類型賦值,如集合、數組、其他對象等
- Person.java
package com.mashibing.bean;
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;
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("有參構造器");
}
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
package com.mashibing.bean;
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
package com.mashibing.bean;
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) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", town='" + town + '\'' +
'}';
}
}
- 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"
>
<!--給複雜類型的賦值都在property標籤內進行-->
<bean id="person" class="com.mashibing.bean.Person">
<property name="name">
<!--賦空值-->
<null></null>
</property>
<!--通過ref引用其他對象,引用外部bean-->
<property name="address" ref="address"></property>
<!--引用內部bean-->
<!-- <property name="address">
<bean class="com.mashibing.bean.Address">
<property name="province" value="北京"></property>
<property name="city" value="北京"></property>
<property name="town" value="西城區"></property>
</bean>
</property>-->
<!--爲list賦值-->
<property name="books">
<list>
<!--內部bean,內部bean無法從ioc容器中直接獲取對象的值,外部bean可以-->
<bean id="book1" class="com.mashibing.bean.Book">
<property name="name" value="多線程與高併發"></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 name="maps">
<map>
<entry key="a" value="aaa"></entry>
<entry key="address" value-ref="address"></entry>
<entry key="address2">
<bean class="com.mashibing.bean.Address">
<property name="province" value="廣東省"></property>
</bean>
</entry>
</map>
</property>
<!--給property賦值-->
<property name="properties">
<props>
<prop key="aaa">aaa</prop>
<prop key="bbb">222</prop>
</props>
</property>
<!--給數組賦值-->
<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>
<!--級聯屬性-->
<bean id="person2" class="com.mashibing.bean.Person">
<property name="address" ref="address"></property>
<property name="address.province" value="北京"></property>
</bean>
<!--util名稱空間創建集合類型的bean,外部bean如果是map、list這種類型,需要使用util命名空間-->
<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>
3.5 "繼承"其他bean的屬性值
- 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>
- 如果不想將某個bean實例化,只用於提供給其他bean繼承其屬性值,可以使用abstract屬性
<!--該bean不會被真正創建對象-->
<bean id="person" class="com.mashibing.bean.Person" abstract="true">
<property name="id" value="1"></property>
<property name="name" value="zhangsan"></property>
<property name="age" value="21"></property>
<property name="gender" value="男"></property>
</bean>
<bean id="person2" class="com.mashibing.bean.Person" parent="person">
<property name="name" value="lisi"></property>
</bean>
3.6 利用外部配置文件
- 在resource中添加dbconfig.properties
username=root
password=123456
url=jdbc:mysql://localhost:3306/demo
driverClassName=com.mysql.jdbc.Driver
- 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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
在加載外部依賴文件的時候需要引入context命名空間
-->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="url" value="${url}"></property>
<property name="driverClassName" value="${driverClassName}"></property>
</bean>
</beans>
- 利用外部配置文件爲屬性賦值的坑
- spring容器在進行啓動時,會讀取當前系統的某些環境變量的配置,而當前系統用戶名使用username表示的
- 如果配置文件中,key起名爲username,會導致${username}將環境變量中,主機名讀取出來,所以爲避免這種情況,配置文件中儘量添加前綴來區分,例如jdbc.username
- 如果通過context:property-placeholder,引入了多個配置文件,只有第一個配置文件中的內容會生效,第二個配置文件中內容無法被讀取到
4 bean對象創建的依賴關係
-
bean對象在創建的時候是按照bean在配置文件的順序決定的
-
可以使用depend-on標籤來決定順序
-
一般在實際工作中,不必在意bean創建的先後順序,需要依賴的對象,在創建完成後,都會進行賦值操作,也就是Person中有address屬性,即使先創建person對象,再創建address對象,後創建的address也會爲person中該屬性賦值
-
ioc.xml
<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>
5 bean的作用域
- bean的作用域可以通過scope進行設置:request、session爲spring 4.x版本內容,表示每次新的請求、會話時,創建一個新的對象,但從來沒用過,所以spring 5.x版本中刪除
- singleton:默認爲單例,容器啓動完成之前就已經創建好對象,每次獲取的對象是同一個
- prototype:每次獲取bean時,容器啓動時不創建,獲取對象的時候創建新對象,每次獲取的對象都不同
- request
- session
- ioc.xml
<bean id="person4" class="com.mashibing.bean.Person" scope="prototype"></bean>
6 利用工廠模式創建bean對象
-
在之前的案例中,所有bean對象的創建都是通過反射得到對應的bean實例,其實在spring中還包含另外一種創建bean實例的方式,就是通過工廠模式進行對象的創建
-
在利用工廠模式創建bean實例的時候有兩種方式
- 靜態工廠:工廠本身不需要創建對象,但是可以通過靜態方法調用,對象=工廠類.靜態工廠方法名()
- 實例工廠:工廠本身需要創建對象,工廠類 工廠對象=new 工廠類;工廠對象.get對象名()
-
PersonStaticFactory.java
package com.mashibing.factory;
import com.mashibing.bean.Person;
public class PersonStaticFactory {
public static Person getPerson(String name){
Person person = new Person();
person.setId(1);
person.setName(name);
return person;
}
}
- ioc.xml
<!--
class:指定靜態工廠類
factory-method:指定哪個方法是工廠方法
-->
<bean id="person5" class="com.mashibing.factory.PersonStaticFactory" factory-method="getPerson">
<!--constructor-arg:可以爲方法指定參數-->
<constructor-arg value="lisi"></constructor-arg>
</bean>
- SpringDemoTest
Person person = (Person) context.getBean("person5",Person.class);
- PersonInstanceFactory.java
package com.mashibing.factory;
import com.mashibing.bean.Person;
public class PersonInstanceFactory {
public Person getPerson(String name){
Person person = new Person();
person.setId(1);
person.setName(name);
return person;
}
}
- ioc.xml
<!--實例工廠使用-->
<!--需要先創建實例工廠類-->
<bean id="personInstanceFactory" class="com.mashibing.factory.PersonInstanceFactory"></bean>
<!--
factory-bean:指定使用哪個工廠實例
factory-method:指定使用工廠實例的哪個方法
-->
<bean id="person6" class="com.mashibing.bean.Person" factory-bean="personInstanceFactory" factory-method="getPerson">
<constructor-arg value="wangwu"></constructor-arg>
</bean>
- SpringDemoTest
Person person = (Person) context.getBean("person6",Person.class);
7 繼承FactoryBean來創建對象
- FactoryBean是Spring規定的一個接口,當前接口的實現類,Spring都會將其作爲一個工廠
- ioc容器啓動的時候不會創建實例,只有在使用的時候纔會創建對象
- MyFactoryBean.java
package com.mashibing.factory;
import com.mashibing.bean.Person;
import org.springframework.beans.factory.FactoryBean;
/**
* 實現了FactoryBean接口的類是Spring中可以識別的工廠類,spring會自動調用工廠方法創建實例
*/
public class MyFactoryBean implements FactoryBean<Person> {
/**
* 工廠方法,返回需要創建的對象
* @return
* @throws Exception
*/
@Override
public Person getObject() throws Exception {
Person person = new Person();
person.setName("maliu");
return person;
}
/**
* 返回創建對象的類型,spring會自動調用該方法返回對象的類型
* @return
*/
@Override
public Class<?> getObjectType() {
return Person.class;
}
/**
* 創建的對象是否是單例對象
* @return
*/
@Override
public boolean isSingleton() {
return false;
}
}
- ioc.xml
<bean id="myfactorybean" class="com.mashibing.factory.MyFactoryBean"></bean>
- SpringDemoTest
Person person = (Person) context.getBean("myfactorybean",Person.class);
System.out.println(person);
8 bean對象的初始化和銷燬方法
- 可以指定創建對象和銷燬對象時,所調用的方法
- Address.java
package com.mashibing.bean;
public class Address {
private String province;
private String city;
private String town;
public Address() {
System.out.println("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) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public void init(){
System.out.println("對象被初始化");
}
public void destory(){
System.out.println("對象被銷燬");
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", town='" + town + '\'' +
'}';
}
}
- ioc.xml
<!--bean生命週期:即bean的創建到銷燬
1. init-method:指定對象創建後調用的方法
2. destroy-method:指定對象銷燬前調用的方法
3. 如果bean是單例,容器在啓動的時候會創建好,關閉的時候會銷燬創建的bean,且由於對象的創建和銷燬都是由容器控制的,所以init-method和destroy-method的功能會生效
4. 如果bean是多例,對象的創建是由容器控制的,獲取的時候創建對象,但銷燬並不由容器控制,而是由垃圾回收器決定,因此只有init-method生效,destroy-method不生效
-->
<bean id="address" class="com.mashibing.bean.Address" init-method="init" destroy-method="destory"></bean>
- MyTest.java
import com.mashibing.bean.Address;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ioc2.xml");
Address address = context.getBean("address", Address.class);
System.out.println(address);
//applicationContext沒有close方法,需要使用具體的子類
//關閉容器的方法
((ClassPathXmlApplicationContext)context).close();
}
}
9 配置bean對象初始化方法的前後處理方法
- spring中存在一個名爲BeanPostProcessor的接口
- 如果將實現了該接口的類,交給spring管理(配置爲bean),那麼調用任何其他bean的初始化方法之前,都會調用該實現類的postProcessBeforeInitialization方法,初始化之後,又會調用該實現類的postProcessAfterInitialization方法
- 即使bean中未配置初始化方法,那麼會在對象調用後,依次調用postProcessBeforeInitialization、postProcessAfterInitialization
- 如果只想對某些bean生效,那麼需要手動在postProcessBeforeInitialization、postProcessAfterInitialization方法中,判斷傳進來的bean對象類型,然後根據不同類型決定如何處理
- MyBeanPostProcessor.java
package com.mashibing.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* 在初始化方法調用之前執行
* @param bean 初始化的bean對象
* @param beanName xml配置文件中的bean的id屬性
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization:"+beanName+"調用初始化前置方法");
return bean;
}
/**
* 在初始化方法調用之後執行
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization:"+beanName+"調用初始化後綴方法");
return bean;
}
}
- ioc.xml
<bean id="myBeanPostProcessor" class="com.mashibing.bean.MyBeanPostProcessor"></bean>
10 spring創建第三方bean對象
- 在Spring中,很多對象都是單實例的,在日常的開發中,我們經常需要使用某些外部的單實例對象,例如數據庫連接池,下面我們來講解下如何在spring中創建第三方bean實例
- 導入數據庫連接池的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>
- 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<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>
- MyTest.java
import com.alibaba.druid.pool.DruidDataSource;
import com.mashibing.bean.Address;
import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;
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());
}
}
11 spring基於xml文件的自動裝配
-
當一個對象中的屬性需要指向另外一個對象的時候,在之前的配置中都是通過property標籤來進行手動配置的,其實在spring中還提供了一個非常強大的功能就是自動裝配,可以按照我們指定的規則自動爲對象的屬性賦值
-
使用autowire進行自動裝配
- default/no:不自動裝配
- byName:按setter方法名的首字母小寫,去ioc容器中查找是否有同名的bean,如果找不到裝配null,如果壓根沒有setter方法,自然而然也是裝配null
- byType:按照類型進行裝配,以屬性的類型作爲查找依據,去ioc容器中查找是否有相同類型的bean
- 如果找不到則裝配null
- 如果有多個類型相同的bean對象,那麼會拋出異常
- 如果Person中有List和Address類型的屬性,而List中又存放了Address類型的對象,那麼List中的Address和Person中的Address都會被自動裝配成功
- 發現使用byType自動裝配Person時,最終打印的Person對象中,包含大量的環境變量信息,這是因爲spring啓動時,會將環境變量信息,創建成一個Map<String,Object>類型以及Properties的bean對象,並對其進行初始化,而Person中,恰好包含Map<String,Object>類型的屬性maps和Properties類型的屬性properties,當使用byType自動裝配時,這些環境變量所在的bean對象,被自動裝配進了Person的這兩個屬性中
- constructor:按照構造器進行裝配
- 如果想裝配成功,那麼該構造器中的所有形參對應的類型,必須都在ioc容器中存在
- 如果按照類型找到了多個,那麼就會查找哪個bean的名和構造器中形參名完全匹配,找到就裝配,找不到就裝配失敗
-
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<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="person" class="com.mashibing.bean.Person" autowire="byName"></bean>
<bean id="person2" class="com.mashibing.bean.Person" autowire="byType"></bean>
<bean id="person3" class="com.mashibing.bean.Person" autowire="constructor"></bean>
</beans>
12 SpEL的使用
- SpEL:全稱Spring Expression Language,是spring的表達式語言,是一個支持運行時查詢和操作對象圖的強大的表達式語言,類似jsp中的EL、JSTL表達式
- 使用#{…}作爲語法規則,所有的大括號中的字符都認爲是SpEL,之前context表達式中,取配置文件中的屬性值使用${},注意與SpEL區分
- ioc.xml
<bean id="person4" class="com.mashibing.bean.Person">
<!--支持任何運算符-->
<property name="age" value="#{12*2}"></property>
<!--可以引用其他bean的某個屬性值-->
<property name="name" value="#{address.province}"></property>
<!--引用其他bean-->
<property name="address" value="#{address}"></property>
<!--調用靜態方法-->
<property name="hobbies" value="#{T(java.util.UUID).randomUUID().toString().substring(0,4)}"></property>
<!--調用非靜態方法-->
<property name="gender" value="#{address.getCity()}"></property>
</bean>