-
シングルトンオブジェクト
誕生:コンテナは出産のオブジェクトを作成するときに
アライブは:限り、コンテナがまだオブジェクトであるとして生きてきました
死:コンテナが破壊されたが、終焉オブジェクト
概要:シングルトンの同じコンテナ、およびライフサイクル
-
オブジェクトの多くの例
誕生:我々は、オブジェクトSpringフレームワークを使用すると、我々が作成生きて:ちょうどuse've生きて
死:オブジェクトは、長い時間ではなく、他のオブジェクトは、Javaコレクションのメカニズムの回復で使用されていない場合には
たとえば、次のようにシングルトンオブジェクト
パッケージcom.xuefei.service.impl; インポートcom.xuefei.service.AccountService; / ** *アカウントのビジネス層の実装クラス * / パブリック クラス AccountServiceImpl 実装AccountServiceの{ 公共 空のinit(){ System.out.printlnは(「オブジェクトの初期化! " ); } パブリックAccountServiceImpl(){ System.out.printlnは( "作成したオブジェクト"!); } 公共 ボイドDestoryは(){ のSystem.out.printlnは( "!を標的「破壊); } 公共 ボイドsaveAccount( ){ } }
< ビーンID = "AccountServiceの" クラス= "com.xuefei.factory.StaticFactory" ファクトリメソッド= "getAccountService" 範囲"シングルトン" = INIT-方法= "INIT" 破壊法= "Destoryは" </> 豆>
パブリック クラスクライアント{ 公共の 静的な 無効メイン(文字列[] args)を{ ClassPathXmlApplicationContextのApp = 新新 ClassPathXmlApplicationContext( "beans.xmlの"); // 我々は閉じる、使用を呼び出す必要があるため AccountServiceのAccountServiceの=(AccountServiceの)app.getBean( "AccountServiceの" ); System.out.println(AccountServiceの); // 手動破壊 app.close(); } }
< ビーンID = "AccountServiceの" クラス= "com.xuefei.factory.StaticFactory" ファクトリメソッド= "getAccountService" スコープ= "プロトタイプ" のinitメソッド= "INIT" 破壊法= "Destoryは" > </ 豆>