この記事では、Spring Bean の基本的な管理について例を示して説明します。参考のために皆さんと共有してください。詳細は次のとおりです。
1. setter メソッドを使用して依存関係の注入を完了する
以下に Bean およびBeans-config.xml ファイルを示します。
public class HelloBean { private String helloWord //...getter メソッドと setter メソッドを省略します。
<?xml version="1.0"coding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans. dtd"> <beans> <bean id="helloBean" > <property name="helloWord"> <value>Hello!Justin!</value> </property> </bean> </beans>
public class SpringDemo { public static void main(String[] args) { Resource rs = new FileSystemResource("beans-config.xml"); BeanFactory Factory = new XmlBeanFactory(rs); helloBean"); System.out.println(hello.getHelloWord()); } }
2. コンストラクター メソッドを使用して注入を完了します。
public class HelloBean { private String name; private String helloWord; // パラメータなしの構築メソッドを使用することをお勧めします public HelloBean(String name, String helloWord) { this.name = name; ; } //...ゲッターメソッドとセッターメソッドを省略します}
<?xml version="1.0"coding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans. dtd"> <beans> <bean id="helloBean" > <constructor-argindex="0"> <value>Justin</value> </constructor-arg> <constructor-argindex="1"> <value>こんにちは</value> </constructor-arg> </bean> </beans>
public class SpringDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext("beans-config.xml"); HelloBean hello = (HelloBean) context.getBean("helloBean"); ("名前: "); System.out.println(hello.getName()); System.out.println(hello.getHelloWord());
3. 属性参照
public class HelloBean { private String helloWord private Date date; //...getter メソッドと setter メソッドを省略します。
<beans> <bean id="dateBean"/> <bean id="helloBean"> <property name="helloWord"> <value>Hello!</value> </property> <property name="date"> < ref bean="dateBean"/> </property> </bean> </beans>
public class SpringDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext("beans-config.xml"); HelloBean hello = (HelloBean) context.getBean("helloBean"); (hello.getHelloWord()); System.out.print(" それです "); System.out.print(hello.getDate()); System.out.println(".");
4.「byType」自動バインディング
「三」の設定ファイルを以下のように変更し、種類ごとのBean属性の自動バインドを完了させます。
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="byType"> <property name="helloWord"> <value>Hello!</value> </property> </bean> </豆>
5.「byName」自動バインディング
「Three」の設定ファイルを以下のように変更し、名前によるBean属性の自動バインドを完了させます。
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="byName"> <property name="helloWord"> <value>Hello!</value> </property> </bean> </豆>
6.「コンストラクター」の自動バインディング
「三」の設定ファイルを以下のように変更し、構築方法に応じたBean属性の自動バインドが完了します。依存関係を確立するとき、Spring コンテナはコンテナ内の Bean インスタンスのタイプと関連するコンストラクタのパラメータのタイプを比較して、タイプが一致している場合は、そのコンストラクタを使用して Bean サンプルが作成されます。 。バインドできない場合は、org.springframework.beans.factory.UnsatisfiedDependencyException 例外がスローされます。
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="constructor"> <property name="helloWord"> <value>Hello!</value> </property> </bean> </豆>
6.「autodetect」自動バインディング
「Three」の構成ファイルを次のように変更して、Bean 属性の自動バインドを完了します。この自動バインディングは、Spring が依存関係の確立を処理するためにコンストラクターを使用しようとすることを意味します。それが機能しない場合は、byType を使用してみてください。依存関係を確立するクラス。
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="autodetect"> <property name="helloWord"> <value>Hello!</value> </property> </bean> </豆>
7. 依存関係のチェック方法
自動バインディングでは、各属性が定義ファイルから設定されているかどうかを明確に確認する方法がないため、特定の依存関係が実際に確立されていることを確認するために、<bean> タグを定義するときに依存関係チェックを設定できます。 dependency-check」には、単純、オブジェクト、すべて、なしの 4 つの依存関係チェック方法があります。
simple: 単純型 (プリミティブ データ型や文字列オブジェクトなど) のプロパティが依存関係を完了しているかどうかのみをチェックします。
オブジェクト: オブジェクト タイプのプロパティが依存関係を完了しているかどうかを確認します。
all: すべての属性が依存関係を完了しているかどうかを確認します。
none: この設定はデフォルト値であり、依存関係がチェックされないことを示します。
<beans> <bean id="dateBean"/> <bean id="helloBean" autowire="autodetect" dependeny-check="all"> <property name="helloWord"> <value>Hello!</value> < /プロパティ> </bean> </beans>
8. コレクションオブジェクトの挿入
配列、リスト、セット、マップなどのコレクション オブジェクトの場合、一部のオブジェクトは注入前にコレクションに入力する必要があります。その後、コレクション オブジェクトが必要な Bean に注入されるときに、Spring の IoC コンテナによって自動的に維持または生成することもできます。コレクション オブジェクトと完全な依存関係の注入。
public class SomeBean { private String[] someStrArray; private Some[] someObjArray; private リスト someMap; public String[] getSomeStrArray { return someStrArray; someStrArray; } public Some[] getSomeObjArray() { return someObjArray; setSomeObjArray(Some[] someObjArray) { this.someObjArray = someObjArray; } public List getSomeList() { return someList; } public void setSomeList(List someList) { return someMap() public void setSomeMap(Map someMap) { this.someMap = someMap } }パブリッククラス; Some { private String name; public String getName() { return name; } public void setName(String name) { this.name = name } public String toString() { return name;
<?xml version="1.0"coding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans. dtd"> <beans> <bean id="some1"> <property name="name"> <value>ジャスティン</value> </property> </bean> <bean id="some2"> <property name="name"> <value>momor</value> </property> </bean> <bean id="someBean"> <property name="someStrArray"> <list> < value>こんにちは</value> <value>ようこそ</value> </list> </property> <property name="someObjArray"> <list> <ref bean="some1"/> <ref bean="some2"/> </list> </property> <property name="someList"> <list> <value>ListTest</value> <ref bean="some1"/> <ref bean="some2" /> </list> </property> <property name="someMap"> <map> <entry key="MapTest"> <value>Hello!Justin!</value> </entry> <entry key="someKey1 "> <ref bean="some1"/> </entry> </map> </property> </bean> </beans>
public class SpringDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "beans-config.xml"); // 配列型を取得します。ステートフル依存関係注入オブジェクト String[] strs = (String[]) someBean.getSomeStrArray(); Some[] somes = (Some[]) someBean.getSomeObjArray(); for(int i = 0; i < strs.length; i++) { System.out.println(strs[i] + "," + somes[i].getName() ); } // リスト型の依存性注入オブジェクトを取得 System.out.println(); someList.size(); i++) { System.out.println(someList.get(i)); } // Map 型の依存性注入オブジェクトを取得します。 someMap = (Map) someBean.getSomeMap (); System.out.println(someMap.get("MapTest")); }
この記事が Java プログラミングの皆様のお役に立てれば幸いです。