blog

Springソースコード学習「Ⅰ」~スプリングコンテナの作成

springbootまたはmavenプロジェクトを作成し、springの依存関係を追加する xmlファイルを準備する いくつかのコードを書く 一般的なメカニズムを理解する xmlファイルまたはアノテー...

Aug 3, 2023 · 2 min. read
シェア
  • 準備
  1. Spring Boot または Maven プロジェクトを作成し、Spring の依存関係を追加します。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
  1. XMLファイルを用意します
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2100"/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd "> <bean id="user" class="com.example.demo.User"> <property name="name" value=" "></property> <property name="pwd" value="123"></property> </bean> <bean id="myBeanFactoryPostProcessor" class="com.example.demo.MyBeanFactoryPostProcessor"></bean> </beans>
  1. コードを記述します
public static void main(String[] args) { // SpringApplication.run(DemoApplication.class, args); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml"); Object user = applicationContext.getBean("user"); System.out.println(user); }
public static void main(String[] args) { // SpringApplication.run(DemoApplication.class, args); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml"); Object user = applicationContext.getBean("user"); System.out.println(user); }
  • 一般的なメカニズムを理解する

統一されたインターフェース仕様を通じて、XMLファイルまたはアノテーション情報を読み込み、beanDefintion情報を生成します。同時に、BeanFactoryPostProcessorを通じて、beanDefintion情報を完全に取得します。beanDefintion情報を取得した後、リフレクションを通じてbeanオブジェクトを取得し、使用することができます。

  • ソースコードから学ぶ

ClassPathXmlApplicationContext('bean.xml');

メインエントリ

    このコンテキストで使用されるビーンファクトリーを準備します。

    prepareBeanFactory(beanFactory);

  • Read next

    ファーウェイAR G3エンタープライズルート - クラウドから生まれ、パフォーマンスが王である!

    AR G3シリーズは、中小企業及び大企業向けのルーター製品として、革新的で高品質な製品を数多く取り揃えており、その中でもAR1200、AR2200、AR2240、AR3260は、革新的な技術的優位性により、ファーウェイの主要モデルとなっており、政府、金融、電力業界において広く注目され、認知されています。

    Jul 31, 2023 · 3 min read