1.通过@Bean(にinitMethod = "initPerson"、destroyMethodを= "destroyPerson" ) パブリック クラス人{ パブリック人(){ System.out.printlnは( "人物初始化............" ); } 公共 ボイドinitPerson(){ System.out.printlnは( "人物..... INIT" )。 } 公共 ボイドdestroyPerson(){ System.out.printlnは( "人物.....破壊" )。 } } 2 。@ PostConstruct和@PreDestroy パブリック クラス豚{ パブリック豚(){ のSystem.out.println("ブタが作成され..." ); } @PostConstruct 公共 ボイドinitPig(){ System.out.printlnは( "豚..... INIT" ); } @PreDestroy 公共 ボイドdestroyPig(){ システム。 out.printlnを( "豚が破壊....." ); } } 。3 。DisposableBean実装インタフェースとInitializingBean パブリック クラス猫実装InitializingBean、DisposableBean { パブリックキャット(){ のSystem.out.printlnは(「猫....ました「初期化); } @Override 公共 空 afterPropertiesSetは()スロー{例外を するSystem.out.println( "猫...のinit" ); } @Override 公共 ボイド)(破壊スロー例外{ (のSystem.out.println "破壊...猫" )。 } } @import({カタログクラス、豚。クラス}) パブリック クラスConfigue { @Bean(にinitMethod = "initPerson"、destroyMethodを= "destroyPerson" ) 公共人の人物(){ 戻り 新しい)人数(。 } } パブリック クラスアプリ { 公共 静的 ボイドメイン(文字列[]引数) { AnnotationConfigApplicationContextのApplicationContext = 新しい AnnotationConfigApplicationContext(Configue。クラス)。 applicationContext.close(); } }
我々はいくつかの助けのためのソースコードを学習し、3通りの方法でこれを理解します