春のコレクション(リスト、セット、マップ、およびプロパティ)の例

Springコレクション(リスト、セット、マップ、およびプロパティ)の例

コレクションタイプ(リスト、セット、マップ、プロパティ)に値を注入する方法を示すSpringの例。 4つの主要なコレクションタイプがサポートされています。

  • リスト–

  • セット–

  • 地図–

  • プロパティ-

春豆

4つのコレクションプロパティを持つCustomerオブジェクト。

package com.example.common;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Customer
{
    private List lists;
    private Set sets;
    private Map maps;
    private Properties pros;

    //...
}


Bean構成ファイルでコレクションを宣言するには、さまざまなコードスニペットを参照してください。

1. リストの例

    
        
            1
            
            
                
                
                
            
        
    

2. セット例

    
        
            1
            
            
                
                
                
            
        
    

3. 地図の例

    
        
            
            
            
                
                    
                    
                    
                
            
        
    

4. プロパティの例

完全なSpringのBean構成ファイル。

それを実行します…

package com.example.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");

        Customer cust = (Customer)context.getBean("CustomerBean");
        System.out.println(cust);

    }
}

出力

Customer [

lists=[
1,
Person [address=address 1, age=28, name=example1],
Person [address=address, age=28, name=exampleList]
],

maps={
key 1=1,
key 2=Person [address=address 1, age=28, name=example1],
key 3=Person [address=address, age=28, name=exampleMap]
},

pros={[email protected], [email protected]},

sets=[
1,
Person [address=address 1, age=28, name=example1],
Person [address=address, age=28, name=exampleSet]]
]

ソースコードをダウンロード

ダウンロード–Spring-Collection-Example.zip(6 KB)