問題:
春のコンテナを注入するために、リスナーを初期化するために、データオブジェクトは、最初にリスナーの初期化データを使用することができます春のコンテナを起動する必要があります。
ソリューション:
春のフレームを開始するために、リスナーを使用します
問題:パラメータを開始する必要がSpringフレームワーク?
1.必要がコンフィギュレーションファイルまたはコンフィギュレーションクラスの場所を指定します
2.あなたが疲れて、注釈の設定を使用している場合は、コンテナの種類を変更する必要があります
質問:リスナーが初期化パラメータをサポートしていません。内部リスナーにパラメータを渡す方法は?
:内部グローバルコンテキストパラメータを再設定することにより、のContextLoaderListenerリスナーは、コンテキストパラメータを取得することによって、この問題を解決することです
< コンテキストPARAM > < PARAM名> contextConfigLocation </ PARAM名//対応するスプリング・コントローラの先端が<PARAM-value>は書き込まないことに注意してください> < PARAM値> org.chu.config </ PARAM値> </ コンテキストPARAM > < 文脈PARAM > < PARAM名> contextClass </ PARAM名> < PARAM値> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </ PARAM値> </ コンテキスト-PARAM > < 聞き手> < リスナクラス> org.springframework.web.context.ContextLoaderListener </ リスナクラス> </ リスナー>
カスタムデータの初期化リスナー
パブリック クラス DataInitializeListener 実装のServletContextListenerのは{ // 春の依存性注入のみSpringコンテナで使用されるオブジェクト:問題は。リスナーは、オブジェクトコンテナの春ではありません。 // 解決方法:容器内のオブジェクトによって得られた非コンテナ・オブジェクト、コンテナオブジェクト.getBean(); / ** *コンテキスト初期化時間メソッド呼び出し * / @Override 公共 ボイドcontextInitialized(ServletContextEvent SCE){ のSystem.out .println( "== ==を開始" ) のServletContextコンテキスト = sce.getServletContext(); // のApplicationContext WebApplicationContextのApplicationContext = WebApplicationContextUtils.getWebApplicationContext(コンテキスト)。 RoleService roleService = applicationContext.getBean(RoleServiceに。クラス); DictionaryService dictionaryService = applicationContext.getBean(DictionaryService。クラス)。 ModularService modularService = applicationContext.getBean(ModularService。クラス)。 PermissionService permissionService = applicationContext.getBean(PermissionService。クラス)。 UserServiceのUserServiceの = applicationContext.getBean(UserServiceのクラス)。 AreaService areaService = applicationContext.getBean(AreaService。クラス)。 CustomerSerivce customerSerivce = applicationContext.getBean(CustomerSerivce。クラス); } / ** *メソッド破壊するときのコールコンテキスト * / @Override 公共 ボイドcontextDestroyed(ServletContextEvent SCE){ するSystem.out.println( "== ==閉" )。 コンテキストのServletContext = sce.getServletContext(); context.removeAttribute( "global_roles" ); context.removeAttribute( "global_dictionarys" ); } }