生活随笔
收集整理的這篇文章主要介紹了
spring学习笔记02-spring-bean创建的细节问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring學習筆記02-spring-bean創建的細節問題
三種創建Bean對象的方式Bean的作用范圍Bean的生命周期
<?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"><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"scope="prototype" init-method="init" destroy-method="destroy"></bean>
</beans>
相關代碼:
ps:關于Bean的創建第二種,第三種方法適用于在無法修改源碼的情況下,比如當我們引用別人寫好的jar包,此時這個jar包中可能沒有相關的構造函數,但是有其他的返回對象的方法。
- 創建bean的第二種方式,“使用普通工廠中的方法創建對象”中的“普通工工廠”
package com
.itheima
.factory
;import com
.itheima
.service
.IAccountService
;
import com
.itheima
.service
.impl
.AccountServiceImpl
;
public class InstanceFactory {public IAccountService
getAccountService(){return new AccountServiceImpl();}
}
- 創建bean的第三種方式,“使用工廠中的靜態方法創建對象”中的“靜態工廠”
package com
.itheima
.factory
;import com
.itheima
.service
.IAccountService
;
import com
.itheima
.service
.impl
.AccountServiceImpl
;
public class StaticFactory {public static IAccountService
getAccountService(){return new AccountServiceImpl();}
}
總結
以上是生活随笔為你收集整理的spring学习笔记02-spring-bean创建的细节问题的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。