注入ライン1.2のセット春の研究ノート

春のコレクション注入:

私たちは、とのテスト実行のために、以下の各クラスでテストクラスを作成します

デモ:

public class Test_Test{
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Test test = ctx.getBean("test",Test.class);
        System.out.println(test);
    }
}

1.注射配列データ:

デモ:テストクラスを作成します。

public class Test{
    private String msg[];
    private int data[];

    public String[] getMsg() {
        return msg;
    }

    public void setMsg(String[] msg) {
        this.msg = msg;
    }

    public int[] getData() {
        return data;
    }

    public void setData(int[] data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "msg=" + Arrays.toString(this.msg)+",data="+Arrays.toString(this.data);
    }
}

二つの配列は、データ・タイプに対応して設けられている配列データの使用において、オブジェクト情報を受信することができます。 

<bean id="test" class="com.project.Test.Test"><!—路径以自己的为准-->
    <property name="data">
        <array value-type="java.lang.Integer">
            <value>10</value>
            <value>20</value>
            <value>30</value>
        </array>
    </property>
    <property name="msg">
        <array value-type="java.lang.String">
            <value>SpringBoot</value>
            <value>Spring</value>
            <value>SpringMVC</value>
        </array>
    </property>
</bean>

  業績Test_Testテストクラスをクリックします:

2.リストオブジェクトの配列を操作するのセット:

public class Item2 {
    private List<String> msg;
    public void setMsg(List<String> msg) {
        this.msg = msg;
    }
    public List<String> getMsg() {
        return msg;
    }
    @Override
    public String toString() {
        return "msg= "+this.msg;
    }
}

ITEM2 applicationContext.xmlをオブジェクト設定ファイル、提供する配列型と値を追加します。 

<bean id="item2" class="com.project.Demo2.Item2">
    <property name="msg">
        <array value-type="java.lang.String">
            <value>"Java工程师"</value>
            <value>"信息安全工程师"</value>
        </array>
    </property>
</bean>

セット注入アウトセットが値を複製すること、および操作方法は、リストと同じです

ItemTestクラスの実行結果を見ます

public class TestItem2 {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Item2 item2 = ctx.getBean("item2",Item2.class);
        System.out.println(item2);
    }
}

3.塗りつぶし地図コレクション

プロパティを作成するために地図を使用して、クラスの動作、より多く: 

public class Item2 {
    private Map<Integer,String> msg;
    public Map<Integer, String> getMsg() {
        return msg;
    }
    public void setMsg(Map<Integer, String> msg) {
        this.msg = msg;
    }    
}

豆のキー内のオブジェクトの設定されたマッププロパティ:

<bean id="item2" class="com.project.Demo2.Item2">
    <property name="msg">
        <map key-type="java.lang.Integer" value-type="java.lang.String">
            <entry key="1" value="Spring"></entry>
            <entry key="2" value="MyBatis"></entry>
        </map>
    </property>
</bean>

4. プロパティインジェクション

注入プロファイルのセットを開発するの製造方法では、最も一般的なタイプのプロパティです。

初期化され、プロパティのプロパティタイプを作成:

public class Item2 {
    //只能设置String类型的数据
    private Properties msg;
    public Properties getMsg() {
        return msg;
    }
    public void setMsg(Properties msg) {
        this.msg = msg;
    }
    @Override
    public String toString() {
        return "msg= "+this.msg;
    }
}

xml ファイルの設定のプロパティ] キーの種類

    <bean id="item2" class="com.project.Demo2.Item2">
        <property name="msg">
            <props>
                <prop key="java">www.java.com</prop>
                <prop key="spring">www.spring.cn</prop>
            </props>
        </property>
    </bean>

テストクラスItemTest業績をクリックします: 

6.Spring 定義を参照する内部文書を達成 

  部門部門のクラスを作成すると、Employeeクラスの内部スタッフの属性への参照を実現します:


public class Detp {
    private int DetpId;
    private String DetpName;
    private List<Employee> employees;
    public List<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public int getDetpId() {
        return DetpId;
    }
    public String getDetpName() {
        return DetpName;
    }
    public void setDetpId(int detpId) {
        DetpId = detpId;
    }
    public void setDetpName(String detpName) {
        DetpName = detpName;
    }
    @Override
    public String toString() {
        return "部门编号: "+this.DetpId+",部门名称: "+this.DetpName+",雇员信息: "+this.employees;
    }
}

その後、XMLが構造的関係プロファイル上述しました

<bean id="dept" class="com.project.Test.Detp">
    <property name="detpName" value="开发部"/>
    <property name="detpId" value="1"/>
    <property name="employees">
	<!--指定数据类型为Employee类-->
        <list value-type="com.project.Test.Employee">	
    <!--使用反射调用员工对象--> 
            <ref bean="empA"/>		
            <ref bean="empB"/>
            <ref bean="empC"/>
        </list>
    </property>
</bean>
<!--设置3个不同的bean对象-->
<bean id="empA" class="com.project.Test.Employee">
    <property name="emp_id" value="15811"/>
    <property name="emp_name" value="EmployeeA"/>
    <!--ref引用其他Bean对象的内容-->
    <property name="detp" ref="dept"/>
</bean>
<bean id="empB" class="com.project.Test.Employee">
    <property name="emp_id" value="15812"/>
    <property name="emp_name" value="EmployeeB"/>
    <property name="detp" ref="dept"/>
</bean>
<bean id="empC" class="com.project.Test.Employee">
    <property name="emp_id" value="15813"/>
    <property name="emp_name" value="EmployeeC"/>
    <property name="detp" ref="dept"/>
</bean>

クリックして従業員のテストクラスの営業成績

 

公開された58元の記事 ウォン称賛31 ビュー40000 +

おすすめ

転載: blog.csdn.net/qq_37504771/article/details/103154234