SpringのIoC(制御の反転)4-XMLインジェクションの実践

前回の記事では、単純な値を注入する方法を学びましたが、注入できるタイプはたくさんあるので、ここで試してみましょう。
例えば、2つのクラスを定義し、User.javaそしてUserAddress.java次の属性定義
でのUser.javaクラス:

private int age;
private String name;
private String[] hobbeys;
private List<String> cards;
private Map<String, String> girlfirends;
private Set<String> houses;
private UserAddress address;

次に、対応するset合計getメソッドを生成します。UserAddress.java定義:

private String address;

同様の生産setgetおよびtoString方法が可能です。
次に、対応するbeans.xmlドキュメントの作成を開始します。公式ドキュメントを参照することをお勧め
ます:https//docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-collection-elements forwriting便利なように、beans.xmlファイルの上部を右クリックするSplit Verticallyと、効果は次のようになります。
ここに画像の説明を挿入します
より便利です。その後、開始できます。

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="weizu" class="com.weizu.pojo.User">
        <property name="age" value="23"></property>
        <property name="name" value="张三"></property>
        <property name="hobbeys">
            <array>
                <value>抽烟</value>
                <value>喝酒</value>
            </array>
        </property>
        <property name="cards">
            <list>
                <value>123131231312</value>
                <value>123131231312</value>
            </list>
        </property>
        <property name="girlfirends">
            <map>
                <entry key="one" value="对象1"/>
                <entry key="two" value="对象2"/>
            </map>
        </property>
        <property name="houses">
            <set>
                <value>北京</value>
                <value>上海</value>
            </set>
        </property>
        <property name="address">
            <null/>
        </property>
    </bean>
</beans>

テスト:

import com.weizu.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myTest {
    
    

    @Test
    public void Test(){
    
    
        // create and configure beans
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User userService = (User) context.getBean("weizu");
        System.out.println(userService.toString());
    }
}

結果:
ここに画像の説明を挿入します


参照ビデオ:https//www.bilibili.com/video/BV1WE411d7Dv?p = 9&spm_id_from = pageDriver
参照ドキュメント:https//docs.spring.io/spring-framework/docs/current/reference/html/core.html #beans-collection-elements

複数の異なるbeans-xxx.xmlファイルがある場合は、合計を定義してXMLから、次のようにimportインポートを使用できます

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

services.xml位置になければならないclasspath location2つの後の位置になければならないが、resources下に、これらのパスは、パスに対するものです。もちろん、このパスは、:file:C:/config/services.xmlclasspath:/config/services.xmlなどの絶対パスとして指定できます
ではSpring IoC容器は、複数のホストすることができbean、次のように、設定時に、データの一部を構成することができます。

  • class資格、指定されたbeanカテゴリ。
  • name指定されたbeanインスタンス化名。
  • scope指定されたbeanアクションの範囲は、任意ありsingleton、、 、prototyperequestデフォルトでは、それぞれが表す単一の容器の存在、オブジェクト・インスタンスを表すことが可能有望な複数のインスタンスを、単一表す単一の要求インスタンスを、同様にまたへシングルインスタンス;媒体内の単一インスタンス意味します;媒体内の単一インスタンス意味します;sessionapplicationwebsocketsingletonSpring IoCbeanprototypebeanrequestHTTPsessionHTTPSessionapplicationServletContextwebsocketWebSocket

具体的な指定方法は以下のとおりです。

<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

おすすめ

転載: blog.csdn.net/qq_26460841/article/details/115041065