1 首先,新建一個maven項目, 在pom.xml中導入spring相關
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
</dependencies>
2 在resources下面新建bean.xml文件, 配置spring的bean容器. resources即爲項目類的根路徑
<?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和class屬性之後,且沒有其他標籤時,使用默認的構造函數創建
(重寫有參構造方法之後,一定要重寫無參構造)
<bean id="accountService" class="com.hr.service.impl.AccountServiceImpl"/>
-->
<!--第二種方法(在jar包中的類的某個方法): 使用普通工廠中的方法創建對象(使用某個類中的方法創建對象,並存入容器中)-->
<!--<bean id="instanceFactory" class="com.hr.factory.InstanceFactory"/>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"/>-->
<!--第三種方法(在jar包中的類的某個方法): 使用工廠中的靜態方法創建對象(使用某個類中的靜態方法創建對象,並存入容器中)-->
<!--<bean id="accountService" class="com.hr.factory.StaticFactory" factory-method="getAccountService"/>-->
<!--
bean對象的生命週期:
單例對象:
出生: 當容器創建時對象出生
活着: 只要容器還在,對象一直活着
死亡: 容器銷燬,對象消亡
死亡: 單例對象的生命週期和容器相同
多例對象:
出生: 當我們get對象的時候(使用對象)時spring框架爲我們創建
活着: 對象只要是在使用過程中就一直活着
死亡: 當對象長時間不用,且沒有別的對象引用時候,由Java的垃圾回收器回收
-->
<bean id="accountService" class="com.hr.service.impl.AccountServiceImpl" scope="prototype"
init-method="init" destroy-method="destory"
/>
</beans>
AccountServiceImpl實體類:
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl() {
System.out.println("AccountServiceImpl創建了");
}
public void saveAccount() {
System.out.println("service中的saveAccount執行了 ");
}
private void init() {
System.out.println("對象初始化了");
}
private void destory() {
System.out.println("對象銷燬了");
}
}
測試類:
package com.hr;
import com.hr.dao.impl.AccountDaoImpl;
import com.hr.service.IAccountService;
import com.hr.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Client {
public static void main(String[] args) {
// testIoc01();
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = (IAccountService) applicationContext.getBean("accountService");
//手動關閉容器
applicationContext.close();
}
private static void testIoc01() {
// /*-------------ApplicationContext-------------*/
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// AccountServiceImpl accountService = (AccountServiceImpl) ac.getBean("accountService");
// AccountDaoImpl accountDao = (AccountDaoImpl) ac.getBean("accountDao");
// System.out.println(accountService);
// System.out.println(accountDao);
// /*-------------BeanFactory-------------*/
Resource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
AccountServiceImpl accountService = (AccountServiceImpl) factory.getBean("accountService");
System.out.println(accountService);
}
}
筆記:
兩個容器:
ApplicationContext: 頂級父類是BeanFactory, 但是創建對象的策略是立即加載的方式,只要一讀完配置文件就馬上創建配置文件中
的對象 (單例對象適用: Servlet, Service等)
BeanFactory: :延遲加載, 什麼時候根據id獲取對象了, 什麼時候才真正的創建對象 (多例對象適用)
Spring對bean的管理細節:
1 創建bean的三種方式
第一種方法: 使用默認構造函數創建
第二種方法: 使用普通工廠中的方法創建對象(使用某個類中的方法創建對象,並存入容器中)
第二種方法: 使用工廠中的靜態方法創建對象(使用某個類中的靜態方法創建對象,並存入容器中)
2 bean對象的作用範圍:
默認是單例的, bean標籤的scope可以調整
scope取值:
singleton: 單例的(默認值)
prototype: 多例的
request: 作用於web應用的請求範圍
session: 作用於web應用的會話範圍
global-session: 作用於集羣環境的會話範圍(全局會話範圍), 當不是集羣環境時,它就是session
3 bean對象的生命週期:
單例對象:
出生: 當容器創建時對象出生
活着: 只要容器還在,對象一直活着
死亡: 容器銷燬,對象消亡
死亡: 單例對象的生命週期和容器相同
多例對象:
出生: 當我們get對象的時候(使用對象)時spring框架爲我們創建
活着: 對象只要是在使用過程中就一直活着
死亡: 當對象長時間不用,且沒有別的對象引用時候,由Java的垃圾回收器回收
代碼託管地址: