spring中bean的细节之三种创建Bean对象的方式
生活随笔
收集整理的這篇文章主要介紹了
spring中bean的细节之三种创建Bean对象的方式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?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>com.learn</groupId><artifactId>day01_learn_04bean</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency></dependencies>
</project>
package com.learn.ui;import com.learn.service.IAccountService;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模擬一個(gè)表現(xiàn)層,用于調(diào)用業(yè)務(wù)層*/
public class Client {/**** @param args*/public static void main(String[] args) {//1.獲取核心容器對(duì)象
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.根據(jù)id獲取Bean對(duì)象IAccountService as = (IAccountService)ac.getBean("accountService");as.saveAccount();//手動(dòng)關(guān)閉容器ac.close();}
}
package com.learn.service;/*** 賬戶業(yè)務(wù)層的接口*/
public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount();
}
package com.learn.service.impl;import com.learn.service.IAccountService;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類*/
public class AccountServiceImpl implements IAccountService {public AccountServiceImpl(){System.out.println("對(duì)象創(chuàng)建了");}public void saveAccount(){System.out.println("service中的saveAccount方法執(zhí)行了。。。");}public void init(){System.out.println("對(duì)象初始化了。。。");}public void destroy(){System.out.println("對(duì)象銷毀了。。。");}}
package com.learn.factory;import com.learn.service.IAccountService;
import com.learn.service.impl.AccountServiceImpl;/*** 模擬一個(gè)工廠類(該類可能是存在于jar包中的,我們無法通過修改源碼的方式來提供默認(rèn)構(gòu)造函數(shù))*/
public class StaticFactory {public static IAccountService getAccountService(){return new AccountServiceImpl();}
}
package com.learn.factory;import com.learn.service.IAccountService;
import com.learn.service.impl.AccountServiceImpl;/*** 模擬一個(gè)工廠類(該類可能是存在于jar包中的,我們無法通過修改源碼的方式來提供默認(rèn)構(gòu)造函數(shù))*/
public class InstanceFactory {public IAccountService getAccountService(){return new AccountServiceImpl();}
}
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--把對(duì)象的創(chuàng)建交給spring來管理--><!--spring對(duì)bean的管理細(xì)節(jié)1.創(chuàng)建bean的三種方式2.bean對(duì)象的作用范圍3.bean對(duì)象的生命周期--><!--創(chuàng)建Bean的三種方式 --><!-- 第一種方式:使用默認(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)建。<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>--><!-- 第二種方式: 使用普通工廠中的方法創(chuàng)建對(duì)象(使用某個(gè)類中的方法創(chuàng)建對(duì)象,并存入spring容器)<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean><bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>--><!-- 第三種方式:使用工廠中的靜態(tài)方法創(chuàng)建對(duì)象(使用某個(gè)類中的靜態(tài)方法創(chuàng)建對(duì)象,并存入spring容器)<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>--><!-- learnbean標(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<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"></bean>--><!-- bean對(duì)象的生命周期單例對(duì)象出生:當(dāng)容器創(chuàng)建時(shí)對(duì)象出生活著:只要容器還在,對(duì)象一直活著死亡:容器銷毀,對(duì)象消亡總結(jié):單例對(duì)象的生命周期和容器相同多例對(duì)象出生:當(dāng)我們使用對(duì)象時(shí)spring框架為我們創(chuàng)建活著:對(duì)象只要是在使用過程中就一直活著。死亡:當(dāng)對(duì)象長時(shí)間不用,且沒有別的對(duì)象引用時(shí),由Java的垃圾回收器回收--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"scope="prototype" init-method="init" destroy-method="destroy"></bean>
</beans>
?
總結(jié)
以上是生活随笔為你收集整理的spring中bean的细节之三种创建Bean对象的方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ioc的概念和作用
- 下一篇: spring的依赖注入