プログラミングマガジン

プログラミングを中心にIT技術をできるだけわかりやすくまとめます。

  • ホーム
  • Java
  • 【Spring Framework】DI(依存性の注入)を実装してみる。(設定ファイルから…
 
 
     
  • サーバー言語  
    • Python
    • Ruby
    • PHP
    • SQL
  •  
  • インフラ  
       
    • AWS
    •  
    • 基本
    • Git
  • Web
       
    • Web開発
    • JavaScript
    • Vue.js
    • React
  •  
  • 設計  
       
    • 実装設計
    • DB設計
  • 問い合わせ
  

【Spring Framework】DI(依存性の注入)を実装してみる。(設定ファイルから)

05.06

  • miyabisan2
  • コメントを書く

この記事は1分で読めます

下記の記事で、DIは、Spring Frameworkのコア機能で、重要な機能ということをご紹介させて頂きました。

【Spring Framework】Springのコア機能(DI :依存性の注入)について

今回は、DIの実装を実際にやってみて、DIがどんなものか直感的に理解をしましょう。

DIの実装

DIの実装には、「Bean」と、「設定ファイル」が必要とご紹介させて頂きました。

Beanの準備

まずは、Beanを作成しましょう。何でもいいですが、下記のようにシンプルな形でBeanを用意しました。

1
2
3
4
5
6
7
8
9
10
11
12
13
public class TestBean{
 
private String helloworld;
 
public String getHelloworld() {
return helloworld;
}
 
public void setHelloworld(String helloworld) {
this.helloworld = helloworld;
}
 
}

設定ファイルの準備

「src/main/resources」を右クリック→New→「Spring Bean Configuration File」をクリックします。

すると下記のようなファイルが作成されます。

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
</beans>

サンプルの設定ファイルとして、下記を入力します。

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
<bean id ="beantest" class="com.springapp.TestBean">
<property name ="helloworld" value ="Hello World!" />
</bean>
 
</beans>

書き方としては、下記のようになります。Beanに注入する値を設定しています。

1
2
3
<bean id ="名前" class="クラス名">
<property name ="プロパティ名" value ="注入する値" />
</bean>

実行ファイルとして、下記を用意します。

上記の設定ファイルで、idとして設定した値を指定すれば、設定ファイルから値を取り出すことができます。

1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Main {
 
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
TestBean beantest = (TestBean)app.getBean("beantest");
System.out.println(beantest.getHelloworld());
}
 
}

プログラムを用意した後は、下記のアプリ構成になっています。

実行結果

下記のように、設定ファイルから読み込まれた文字列が、出力されていることがわかります。

スポンサーリンク
  • 2018 05.06
  • miyabisan2
  • コメントを書く
  • Java
  • Tweets Twitter
  • このエントリーをはてなブックマークに追加
  • LINEで送る

関連記事

  1. 2018 05.04

    【Java】オブジェクト指向:継承の問題点を解決する「抽象クラス」、「インターフェース」

  2. 2018 04.18

    【JSP】構成要素(コア要素、ディレクティブ、アクション)

  3. 2018 06.20

    【Struts】「カスタムタグ」の種類

  4. 2018 04.22

    【Java】JDBCプログラムを理解する。

  5. 2018 05.04

    【Java】ビルドツールの基本的な役割等

  6. 2018 04.07

    【Javaサーブレット】doGet、doPostの使い分け

  • コメント ( 0 )
  • トラックバック ( 0 )
  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

返信をキャンセルする。

【UML】UMLの種類、クラス図の基本

【Spring Framework】DI(依存性の注入…

RETURN TOP

著者プロフィール

エンジニア歴10年で過去に業務系、Webデザイン、インフラ系なども経験あります。現在はWeb系でフロントエンド開発中心です。

詳細なプロフィールはこちら

スポンサーリンク

カテゴリー

  • Android
  • AngularJS
  • API
  • AWS
  • C++
  • CSS
  • cursor
  • C言語
  • DDD
  • DevOps
  • Django
  • Docker
  • Figma
  • Git
  • GitLab
  • GraphQL
  • gRPC
  • Hasura
  • Java
  • JavaScript
  • Kubernetes
  • Laravel
  • linux
  • MySQL
  • Next.js
  • nginx
  • Node.js
  • NoSQL
  • Nuxt.js
  • Oracle
  • PHP
  • Python
  • React
  • Redux
  • Rspec
  • Ruby
  • Ruby on Rails
  • Sass
  • Spring Framework
  • SQL
  • TypeScript
  • Unity
  • Vue.js
  • Webサービス開発
  • Webデザイン
  • Web技術
  • インフラ
  • オブジェクト指向
  • システム開発
  • セキュリティ
  • その他
  • データベース
  • デザインパターン
  • テスト
  • ネットワーク
  • プログラミング全般
  • マイクロサービス
  • マイクロソフト系技術
  • マルチメディア
  • リファクタリング
  • 副業
  • 未分類
  • 業務知識
  • 生成AI
  • 設計
  • 関数型言語
RETURN TOP

Copyright ©  プログラミングマガジン | プライバシーポリシー