IoC の紹介
通常の Java 開発では、プログラマは特定のクラス内の他のクラスのメソッドに依存する必要があります。
通常、new はクラスに依存し、クラス インスタンスを呼び出すメソッドです。この種の開発の問題は、新しいクラス インスタンスを均一に管理するのが難しいことです。
Spring は依存性注入のアイデアを提案しています。つまり、依存するクラスはプログラマによってインスタンス化されませんが、Spring コンテナは新しいインスタンスを指定し、そのオブジェクトを必要とするクラスにインスタンスを注入するのに役立ちます。
依存性注入の別の用語は、「制御の反転」です。一般的な理解は、通常は新しいインスタンスを作成し、このインスタンスの制御はプログラマであるというものです。
制御の反転とは、新しいインスタンスの作業がプログラマによって行われず、Spring コンテナに渡されることを意味します。
Spring には複数の形式の依存関係注入があります。この記事では、Spring が XML を通じて IOC 構成を実行する方法のみを紹介します。
1.Set注入
これは、SpringActionがあり、クラス内でSpringDaoオブジェクトをインスタンス化する必要があると仮定します。次に、プライベートSpringDaoメンバー変数を定義して、SpringDao setメソッドを作成します。 (これが ioc のインジェクションの入り口です):
次に、
<!--配置bean,配置后该类由spring管理--> <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> <!--(1)依赖注入,配置当前类中相应的属性--> <property name="springDao" ref="springDao"></property> </bean> <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>
2. コンストラクターのインジェクション
この方法でのインジェクションは、パラメーターを使用したコンストラクターのインジェクションを指します。以下の例を見てください。SpringDao と 2 つのメンバー変数を作成しました。ただし、オブジェクトの set メソッドが設定されていないため、最初の注入メソッドはサポートされません。ここでの注入メソッドは、SpringAction のコンストラクターに注入することです。つまり、SpringAction オブジェクトを作成するときに、2 つのパラメーターの値を設定します。 SpringDao と User を渡す必要があります:
public class SpringAction { //注入对象springDao private SpringDao springDao; private User user; public SpringAction(SpringDao springDao,User user){ this.springDao = springDao; this.user = user; System.out.println("构造方法调用springDao和user"); } public void save(){ user.setName("卡卡"); springDao.save(user); } }
XML ファイルでは、
<!--配置bean,配置后该类由spring管理--> <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> <!--(2)创建构造器注入,如果主类有带参的构造方法则需添加此配置--> <constructor-arg ref="springDao"></constructor-arg> <constructor-arg ref="user"></constructor-arg> </bean> <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean> <bean name="user" class="com.bless.springdemo.vo.User"></bean>
コンストラクター パラメーターの不確実性を解決するには、コンストラクターによって渡される 2 つのパラメーターが同じ型であることが判明する場合があります。どちらに対応する値を割り当てるかを区別するには、いくつかの小さな処理を行う必要があります:
次のパラメータの位置であるインデックスを設定することです:
<bean name="springAction" class="com.bless.springdemo.action.SpringAction"> <constructor-arg index="0" ref="springDao"></constructor-arg> <constructor-arg index="1" ref="user"></constructor-arg> </bean>
もう1つは、パラメータを設定することです。パラメータの型:
<constructor-arg type="java.lang.String" ref=""/>
3. 静的ファクトリのメソッド注入
名前が示すように、静的ファクトリは Spring に管理させるために必要なオブジェクトを取得するために静的ファクトリ メソッドを呼び出します。すべてのオブジェクトでは、「エンジニアリング クラス。静的メソッド ()」を通じてオブジェクトを直接取得することはできませんが、スプリング インジェクションを通じて取得できます。
package com.bless.springdemo.factory; import com.bless.springdemo.dao.FactoryDao; import com.bless.springdemo.dao.impl.FactoryDaoImpl; import com.bless.springdemo.dao.impl.StaticFacotryDaoImpl; public class DaoFactory { //静态工厂 public static final FactoryDao getStaticFactoryDaoImpl(){ return new StaticFacotryDaoImpl(); } }
ここでは、FactoryDao オブジェクトをインジェクトする必要があります。最初の注入とまったく同じに見えますが、その後の XML を見ると大きな違いがわかります:
public class SpringAction { //注入对象 private FactoryDao staticFactoryDao; public void staticFactoryOk(){ staticFactoryDao.saveFactory(); } //注入对象的set方法 public void setStaticFactoryDao(FactoryDao staticFactoryDao) { this.staticFactoryDao = staticFactoryDao; } }
Spring の IOC 設定ファイルに注目してください
<!--配置bean,配置后该类由spring管理--> <bean name="springAction" class="com.bless.springdemo.action.SpringAction" > <!--(3)使用静态工厂的方法注入对象,对应下面的配置文件(3)--> <property name="staticFactoryDao" ref="staticFactoryDao"></property> </property> </bean> <!--(3)此处获取对象的方式是从工厂类中获取静态方法--> <bean name="staticFactoryDao" class="com.bless.springdemo.factory.DaoFactory" factory-method="getStaticFactoryDaoImpl"></bean>
4.インスタンス ファクトリのメソッド インジェクション
インスタンス ファクトリは、オブジェクト インスタンスを取得するメソッドが静的ではないことを意味するため、最初に新しいファクトリ クラスを作成してから、通常のインスタンス メソッドを呼び出す必要があります:
public class DaoFactory { //实例工厂 public FactoryDao getFactoryDaoImpl(){ return new FactoryDaoImpl(); } }
その後、以下のクラス 言うことはありません。前のクラスと非常によく似ていますが、インスタンス ファクトリ クラスを通じて FactoryDao オブジェクトを作成する必要があります:
public class SpringAction { //注入对象 private FactoryDao factoryDao; public void factoryOk(){ factoryDao.saveFactory(); } public void setFactoryDao(FactoryDao factoryDao) { this.factoryDao = factoryDao; } }
最後に、スプリング設定ファイルを見てください:
<!--配置bean,配置后该类由spring管理--> <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> <!--(4)使用实例工厂的方法注入对象,对应下面的配置文件(4)--> <property name="factoryDao" ref="factoryDao"></property> </bean> <!--(4)此处获取对象的方式是从工厂类中获取实例方法--> <bean name="daoFactory" class="com.bless.springdemo.factory.DaoFactory"></bean> <bean name="factoryDao" factory-bean="daoFactory" factory-method="getFactoryDaoImpl"></bean>
5. まとめ
Spring 最も一般的に使用される IOC 注入方法は (1) と (2) です。
また、Spring を通じて作成されたオブジェクトはデフォルトでシングルトンであることにも注意してください。複数インスタンスのオブジェクトを作成する必要がある場合は、
<bean name="..." class="..." scope="prototype">
上記は内容全体です。この記事が皆さんのお役に立てば幸いです。皆さんも PHP 中国語 Web サイトをサポートしていただければ幸いです。
Spring の依存関係注入のいくつかの方法に関連するその他の記事については、PHP 中国語 Web サイトに注目してください。

Javaは、Java Virtual Machines(JVMS)とBytecodeに依存している「Write and Averywherewherewherewherewherewherewhere」の哲学のために、プラットフォームに依存しません。 1)Javaコードは、JVMによって解釈されるか、地元でその場でコンパイルされたBytecodeにコンパイルされます。 2)ライブラリの依存関係、パフォーマンスの違い、環境構成に注意してください。 3)標準ライブラリを使用して、クロスプラットフォームのテストとバージョン管理がプラットフォームの独立性を確保するためのベストプラクティスです。

java'splatformindepenceisnotsimple; itinvolvescomplexities.1)jvmcompatibilitymustbeensuredacrosplatforms.2)nativeLibrariesandsystemCallSneedCarefulHandling.3)依存症の依存症の依存症と依存症の依存症と依存関係の増加 - プラットフォームのパフォーマンス

java'splatformentedentencebenefitswebapplicationsbyAllowingCodeTorunOnySystemwithajvm、simpledifyifieddeploymentandscaling.itenables:1)easydeploymentddifferentservers、2)Seamlessscalingacroscloudplatforms、および3)deminvermentementmentmentmentmentementtodeploymentpoce

jvmistheruntimeenvironment forexecutingjavabytecode、Curivalforjavaの「writeonce、runanywhere」capability.itmanagesmemory、executessuressecurity、makingestessentionentionalforjavadevadedertionserstunterstanderforeffication devitivationdevation

JavareMainsAtopChoiceFordevelopersDuetoitsPlatformEndepentence、Object-OrientedDesign、stryngting、automaticmemorymanagement、およびcomprehensivestandardlibrary.thesefeaturesmavaversatilatileandpowerful、sustableforawiderangeofplications、daspitesomech

java'splatformentencemeansdeveloperscancancodecodeonceanddevicewithoutrocompilling.cancodecodecodecodecodecodecodecodecodecodecodecode compilling

JVMをセットアップするには、次の手順に従う必要があります。1)JDKをダウンロードしてインストールする、2)環境変数を設定する、3)インストールの確認、4)IDEを設定する、5)ランナープログラムをテストします。 JVMのセットアップは、単に機能するだけでなく、メモリの割り当て、ガベージコレクション、パフォーマンスチューニング、エラー処理の最適化を行い、最適な動作を確保することも含まれます。

toensurejavaplatformindopendence、soflowthesesteps:1)compileandrunyourapplicationOnMultiplePlatformsusingDifferentosAndjvversions.2)utilizeci/cdpipelines


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。
