Java クラスが Spring で Bean を取得する 5 つの方法

高洛峰
リリース: 2017-01-23 11:00:24
オリジナル
1685 人が閲覧しました

Spring で Bean を取得するには多くの方法があります。 もう一度要約すると、
最初の方法: 初期化中に ApplicationContext オブジェクトを保存します

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
ログイン後にコピー

注: この方法は Spring フレームワークを使用する独立したアプリケーションに適しており、プログラムが手動で Bean を渡す必要があります。設定ファイル Spring を初期化します。
2 番目: Spring が提供するツール クラスを通じて ApplicationContext オブジェクトを取得します

import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
ログイン後にコピー

説明:
1. これら 2 つのメソッドは、Spring フレームワークを使用して ApplicationContext オブジェクトを取得し、次に、必須のクラス インスタンス
2. 最初のメソッドは取得が失敗すると例外をスローし、2 番目のメソッドは null を返します。
3番目のメソッド: 抽象クラス ApplicationObjectSupport から継承

説明: ApplicationContext インスタンスは、抽象クラス ApplicationObjectSupport が提供する getApplicationContext() メソッドを通じて簡単に取得でき、Spring コンテナ内の Bean を取得できます。 Spring が初期化されると、抽象クラスの setApplicationContext(ApplicationContext context) メソッドを通じて ApplicationContext オブジェクトが挿入されます。
4 番目のメソッド: 抽象クラス WebApplicationObjectSupport から継承

説明: 上記のメソッドと同様に、getWebApplicationContext() を呼び出して WebApplicationContext インスタンスを取得します。
5 番目のメソッド: ApplicationContextAware インターフェイスを実装します

説明: setApplicationContext(ApplicationContext context) メソッドを実装します。このインターフェイスの を作成し、ApplicationContext オブジェクトを保存します。 Spring が初期化されると、ApplicationContext オブジェクトがこのメソッドを通じて挿入されます。 Spring では、Spring の ApplicationContext オブジェクトを取得するために、通常のクラスに対応するクラスまたはインターフェイスを継承または実装するための最後の 3 つのメソッドが用意されていますが、これらの抽象クラスまたはインターフェイスを継承または実装して使用する場合は、通常の Java に注意する必要があります。 Spring の構成ファイル (つまり、application-context.xml ファイル) で構成する必要があります。そうしないと、取得された ApplicationContext オブジェクトは null になります。

以下は、ApplicationContextAware インターフェースを実装することによって Spring コンテナーで Bean を取得する方法を示しています。

まず、ApplicationContextAware インターフェースを実装するクラスをカスタマイズし、内部のメソッドを実装します。

package com.ghj.tool;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
public class SpringConfigTool implements ApplicationContextAware {// extends ApplicationObjectSupport{
 
 private static ApplicationContext ac = null;
 private static SpringConfigTool springConfigTool = null;
 
 public synchronized static SpringConfigTool init() {
 if (springConfigTool == null) {
  springConfigTool = new SpringConfigTool();
 }
 return springConfigTool;
 }
 
 public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
 ac = applicationContext;
 }
 
 public synchronized static Object getBean(String beanName) {
 return ac.getBean(beanName);
 }
}
ログイン後にコピー


次に、applicationContext で構成します。 xml ファイル:

<bean id="SpringConfigTool" class="com.ghj.tool.SpringConfigTool"/>
ログイン後にコピー

最後に、次のコードを通じて Spring コンテナ内の対応する Bean を取得できます:

SpringConfigTool.getBean("beanId");
ログイン後にコピー

サーバーが Spring コンテナの初期化を開始するとき、次のメソッドでは Spring コンテナを取得できないことに注意してください:

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
  
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
wac.getBean(beanID);
ログイン後にコピー

以上がこの記事の内容です 全ての内容が、皆様の学習のお役に立てれば幸いです。

Java クラスが Spring に Bean を取得する 5 つの方法に関するその他の関連記事については、PHP 中国語 Web サイトに注目してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!