SSM框架
- Spring + springMVC + mybatis
作用
- 管理對象: 當(dāng)開發(fā)人員需要某一個類的對象時,不需要自行new對象,而是通過spring直接獲取即可
使用
【掌握】通過spring獲取存在無參構(gòu)造方法類的對象
創(chuàng)建Maven Project
當(dāng)項目創(chuàng)建好之后,生成web.xml,解決默認(rèn)提示錯誤
選擇tomcat, 項目右擊 - > properties -> Target Runtimes
打開
http://mvnrepository.com,搜索springwebmvc,在結(jié)果中找到的Group是org.springframework,選擇版本,并且復(fù)制xml代碼
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
- 配置spring的配置文件(applicationContext.xml)
<!-- id: 自定義名稱
class : 需要spring管理的類的路徑
-->
<bean id="date" class="java.util.Date"></bean>
- 測試
import java.util.Date;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo1 {
public static void main(String[] args) {
//spring的配置文件
String conf="applicationContext.xml";
//獲取spring容器
AbstractApplicationContext context=new ClassPathXmlApplicationContext(conf);
//獲取配置文件中指定的bean,參數(shù)是自定義的id
Date date=(Date) context.getBean("date");
//打印出日期,對象創(chuàng)建成功
System.out.println(date);
context.close();
}
}
內(nèi)存泄露或者內(nèi)存溢出
當(dāng)需要釋放某個對象所占用的內(nèi)存空間時,如果對象沒有正確關(guān)閉,將導(dǎo)致無法釋放,由于這個對象可能已經(jīng)沒有了引用,這個對象再也無法使用,卻一直被誤認(rèn)為被使用,就會變成長期存在于內(nèi)存中的垃圾數(shù)據(jù),就是內(nèi)存泄露
其實少量的內(nèi)存泄露是滅有危害的。但是如果存在大量的內(nèi)存泄露,就可導(dǎo)致可用內(nèi)存明顯變少,計算機的運行性能就會下降,當(dāng)內(nèi)存泄露到極點的時候,就會溢出。盡管少量的內(nèi)存泄露是沒有危害的,但是應(yīng)該嚴(yán)謹(jǐn)?shù)木幊?,盡量不要出現(xiàn)內(nèi)存泄露
【了解】通過spring獲取類中不存在無參構(gòu)造方法,但是存在靜態(tài)工廠方法類的對象
我們使用spring獲取
java.util.Calendar的對象factory-method: 這個屬性指定的靜態(tài)工廠方法在spring的配置文件中配置這個對象
<!-- 通過靜態(tài)工廠方法創(chuàng)建對象
id : 自定義的名稱
class: 類的全路徑
factory-method : 靜態(tài)工廠方法
-->
<bean id="calendar" class="java.util.Calendar" factory-method="getInstance"></bean>
- 測試
@Test
public void testStatice() {
// spring的配置文件
String conf = "applicationContext.xml";
// 獲取spring容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
conf);
// 獲取配置文件中指定的bean,參數(shù)是自定義的id
Calendar calendar=(Calendar) context.getBean("calendar");
// 打印出日期,對象創(chuàng)建成功
System.out.println(calendar.getTime());
context.close();
}
【了解】類中不存在無參構(gòu)造方法,也沒有靜態(tài)工廠方法,但是存在實例工廠方法
實例工廠方法
- 實例工廠方法: 指另一個類中有工廠方法,可以獲取目標(biāo)類型的對象,即X類中有工廠方法(非靜態(tài)的)可以獲取Y類的對象
實例
假設(shè)存在PhoneFactory類中,該類中有非靜態(tài)方法getPhone()可以獲取Phon類型的對象,并且Phone沒有無參構(gòu)造方法
Phone
public class Phone {
public String name;
public Phone(String name) {
this.name=name;
}
}
- PhoneFactory
public class PhoneFactory {
public Phone getPhone() {
return new Phone("小米6");
}
}
- spring配置文件
-
factory-bean: 是工廠類的id -
factory-method: 工廠類獲取Phone對象的非靜態(tài)的方法
-
<!-- 配置工廠類 -->
<bean id="phoneFactory" class="cn.tedu.spring.beans.PhoneFactory"></bean>
<!-- 配置Phone類的對象
factory-bean : 是工廠類的id
factory-method : 工廠類獲取Phone對象的非靜態(tài)的方法
-->
<bean id="phone" class="cn.tedu.spring.beans.Phone" factory-bean="phoneFactory" factory-method="getPhone"></bean>
Bean的作用域(Scope)
- 默認(rèn)情況下,由spring配置的對象是單例的
- 在配置時,在
<bean>節(jié)點添加scope屬性即可調(diào)整,當(dāng)該屬性為singleton時是單例的,當(dāng)屬性為prototype為非單例的
<!-- id: 自定義名稱
class : 需要spring管理的類的路徑
-->
<bean id="date" class="java.util.Date" scope="prototype"></bean>
單例(Singleton)
懶加載
- 在默認(rèn)情況下,spring創(chuàng)建對象的是使用餓漢式,即是在spring配置文件開始加載的時候就創(chuàng)建對象,但是我們可以使用
lazy-init取值我true的時候,就會使用懶加載(懶漢式)
<bean id="date" class="java.util.Date" scope="singleton" lazy-init="true"></bean>
prototype
- 一個Bean定義對應(yīng)多個對象實例
request
- 在一次Http請求中,一個Bean只創(chuàng)建一個實例,僅限于web環(huán)境
session
- 在一個HttpSession中,一個Bean定義對應(yīng)一個實例
globalSession
- 在一個全局的HttpSession中,一個bean定義對應(yīng)一個實例
Bean的延遲初始化
在spring創(chuàng)建Bean的實例的時候默認(rèn)是使用單例,并且是餓漢式加載,即是在spring的配置文件在開始加載的時候就創(chuàng)建bean的實例對象
但是我們可以使用
lazy-init來延遲初始化,使用懶加載即可,當(dāng)lazy-init為true的時候便是延遲加載
<bean id="date" class="java.util.Date" lazy-init="true"></bean>
- 我們還可以在
<beans>根節(jié)點中添加一個default-lazy-init,可以為容器中的所有bean設(shè)置為懶加載
<beans default-lazy-init="true"></beans>
spring配置文件的全部約束
<?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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
</beans>