ホームページ  >  記事  >  Java  >  SpringBoot が applicationContext.xml 構成ファイルを使用する方法

SpringBoot が applicationContext.xml 構成ファイルを使用する方法

WBOY
WBOY転載
2023-06-02 21:13:321932ブラウズ

applicationContext.xml 構成ファイルを使用する

SpringBoot はデフォルトで Java コードによる依存関係注入を行いますが、@ImportResource アノテーションである XML 形式での依存関係注入の入り口も提供します。

このアノテーションを SpringBoot スタートアップ クラスに追加し、アノテーションの location 属性で XML 構成ファイルを指定できます。 (ファイル コレクションを使用することも、メイン構成ファイルのみを導入し、メイン構成ファイル内のタグを使用して他のサブ構成ファイルを導入することもできます。個人的には、2 番目の方法を好みます)。

これにより、コンテナ起動時にxmlファイルに設定されたBeanDefinitionも解析することができます。

applicationContext は設定ファイルをロードします。

ApplicationContext は Spring コンテナのコンテキストとして理解され、コンテキストを通じてコン​​テナ内の Bean を操作します。

  • ClassPathXmlApplicationContext :クラスパスの下に設定ファイルをロードしてコンテナ インスタンスを作成します

  • FileSystemXmlApplicationContext:ファイル内の任意のディレクトリの下に設定ファイルをロードしますシステムを作成し、コンテナ インスタンスを作成します

Case

/*方式一 :ClassPathXmlApplicationContext*/
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");
/*方式二 FileSystemXmlApplicationContext */
        //FileSystemXmlApplicationContext ioc= new FileSystemXmlApplicationContext("E://1804_2//20180827spring//config//spring.xml");
        User u = (User) ioc.getBean("user1");
        System.out.println(u);

複数のファイルのロード方法

/*方式一*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml,spring-mvc.xml");
/*方式二*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String[]{"spring.xml,spring-mvc.xml"});
/*方式三*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-*.xml");
/*方式四*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String []{"classpath:spring-*.xml","mybatis.xml"});
/*方式五*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:*.xml");
/*方式六*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath*:*.xml");
/*方式七*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String []{"classpath:*.xml","classpath:springmvc/beans.xml"});

以上がSpringBoot が applicationContext.xml 構成ファイルを使用する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。