03-spring bean
目錄
- 一、spring 基于 xml 的 IOC 環(huán)境搭建和入門
- 1.pom.xml
- 2.類
- 3. bean.xml
- 4.主函數(shù)
- 5.總結(jié)
- 二、BeanFactory和ApplicationContext的區(qū)別
- 三、spring 中 bean 的細(xì)節(jié)之三種創(chuàng)建 Bean 對(duì)象的方式
- 1. 第一種方式:使用默認(rèn)構(gòu)造函數(shù)創(chuàng)建。
- 2.第二種方式:
- 3.第三種方式:
- 四、spring中bean的細(xì)節(jié)之作用范圍
- 1.bean標(biāo)簽訂單scope屬性:
- 2.bean 對(duì)象的生命周期
- Ⅰ單例對(duì)象
- Ⅱ多例對(duì)象
一、spring 基于 xml 的 IOC 環(huán)境搭建和入門
1.pom.xml
<?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>spring02</groupId><artifactId>spring02</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</version></dependency></dependencies></project>2.類
將上次的耦合和解耦的筆記放進(jìn)去
3. bean.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--把對(duì)象的創(chuàng)建交給 spring 來管理--><bean id="accountService" class="com.service.impl.AccountServiceImpl"/><bean id="accountDao" class="com.dao.impl.AccountDaoImpl"/> </beans>4.主函數(shù)
public class Client {/*** 獲取 spring 的 ioc 核心容器,并根據(jù) id 獲取對(duì)象* @param args*/public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");IAccountService accountService=(IAccountService)ac.getBean("accountService");IAccountDao accountDao=ac.getBean("accountDao",IAccountDao.class);System.out.println(accountService);System.out.println(accountDao);} }5.總結(jié)
ApplicationContext的三個(gè)常用實(shí)現(xiàn)類:
ClassPathXmlApplicationContext:他可以加載類路徑下的配置文件,要求配置文件必須在類路徑下,不在的話,加載不了。
AnnotationConfigApplicationContext:它是用于讀取注解創(chuàng)建容器的。
獲取配置文件對(duì)應(yīng)實(shí)現(xiàn)類有兩種方法:
- IAccountService accountService=(IAccountService)ac.getBean("accountService");
二、BeanFactory和ApplicationContext的區(qū)別
public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");IAccountService accountService=(IAccountService)ac.getBean("accountService");IAccountDao accountDao=ac.getBean("accountDao",IAccountDao.class);System.out.println(accountService);System.out.println(accountDao);/*-------BeanFactory-------------*/Resource resource=new ClassPathResource("bean.xml");BeanFactory beanFactory=new XmlBeanFactory(resource);IAccountService as=(IAccountService)beanFactory.getBean("accountService");System.out.println(as);} }核心容器接口兩個(gè)引發(fā)出的問題:
ApplicationContext: (單例對(duì)象適用)
他在創(chuàng)建核心容器時(shí),創(chuàng)建對(duì)象采取的策略是采用立即加載的方式。也就是說,只要一讀完配置文件馬上就創(chuàng)建配置文件的對(duì)象。
BeanFactory: (多例對(duì)象適用)
他在創(chuàng)建核心容器時(shí),創(chuàng)建對(duì)象采取的策略是采用延遲加載的方式。也就是說,什么時(shí)候根據(jù) id 獲取對(duì)象了,什么時(shí)候才真正的創(chuàng)建對(duì)象。
三、spring 中 bean 的細(xì)節(jié)之三種創(chuàng)建 Bean 對(duì)象的方式
1. 第一種方式:使用默認(rèn)構(gòu)造函數(shù)創(chuàng)建。
- 在spring的配置文件中使用bean標(biāo)簽,配以id和class屬性之后,且沒有其他屬性和標(biāo)簽時(shí)。 采用的就是默認(rèn)構(gòu)造函數(shù)創(chuàng)建bean對(duì)象,此時(shí)如果類中沒有默認(rèn)構(gòu)造函數(shù),則對(duì)象無法創(chuàng)建
2.第二種方式:
- 使用普通工廠中的方法創(chuàng)建對(duì)象(使用某個(gè)類中的方法創(chuàng)建對(duì)象,并存入spring容器)
3.第三種方式:
- 使用工廠中的靜態(tài)方法創(chuàng)建對(duì)象(使用某個(gè)類中的靜態(tài)方法創(chuàng)建對(duì)象,并存入spring容器)
四、spring中bean的細(xì)節(jié)之作用范圍
bean 的作用范圍調(diào)整
1.bean標(biāo)簽訂單scope屬性:
作用:用于指定bean作用范圍 取值:常用的就是單例的于多例的
singleton:單例的(默認(rèn)值)
prototype:多例的
request:作用與web應(yīng)用的請(qǐng)求范圍
session:作用于web應(yīng)用的會(huì)話范圍
global-session:作用于集群環(huán)境的會(huì)話范圍(全局會(huì)話范圍),當(dāng)不是集群環(huán)境時(shí),它就是session
對(duì)于 global-session 的理解
global session 可以理解為全局 session
2.bean 對(duì)象的生命周期
Ⅰ單例對(duì)象
出生:當(dāng)容器創(chuàng)建時(shí)對(duì)象出生
活著:只要容器還在,對(duì)象一直活著
死亡:單例對(duì)象的生命周期和容器相同多例對(duì)象
實(shí)現(xiàn)類
public class AccountServiceImpl implements IAccountService {private IAccountDao accountDao=new AccountDaoImpl();public void saveAccount() {accountDao.saveAccount();}public void init(){System.out.println("對(duì)象初始化");}public void destroy(){System.out.println("對(duì)象銷毀");} }xml 文件
<bean id="accountService" class="com.service.impl.AccountServiceImpl" scope="singleton"init-method="init" destroy-method="destroy"/><bean id="accountDao" class="com.dao.impl.AccountDaoImpl"/>主函數(shù)
public class Client {public static void main(String[] args) {ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");IAccountService as=ac.getBean("accountService",IAccountService.class);as.saveAccount();ac.close();} }運(yùn)行結(jié)果
對(duì)象初始化 保存成功 對(duì)象銷毀Ⅱ多例對(duì)象
出生:當(dāng)我們使用時(shí) spring 框架為我們創(chuàng)建
活著:對(duì)象只要是在使用過程中就一直活著
死亡:當(dāng)對(duì)象長(zhǎng)時(shí)間不用,且沒有別的對(duì)象引用時(shí),由Java的垃圾回收器回收
轉(zhuǎn)載于:https://www.cnblogs.com/zuiren/p/11415413.html
總結(jié)
以上是生活随笔為你收集整理的03-spring bean的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 04-spring的依赖注入
- 下一篇: 吐槽express 中间件multer