【Java】Spring中注入List,Set,Map,Properties

434 阅读9分钟
原文链接: click.aliyun.com
  1. 云栖社区>
  2. 博客列表>
  3. 正文

Spring中注入List,Set,Map,Properties

泳泳啊泳泳 2018-01-08 12:29:59 浏览272 评论0

java spring class schema bean list arraylist

摘要: 下面的例子展示了如何注入 List – <list/> Set – <set/> Map – <map/> Properties – <props/> Spring beans 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.

下面的例子展示了如何注入

  • List – <list/>
  • Set – <set/>
  • Map – <map/>
  • Properties – <props/>

Spring beans








import java.util.List;




import java.util.Map;




import java.util.Properties;




import java.util.Set;




  



public class Customer



{



    private List<Object> lists;




    private Set<Object> sets;




    private Map<Object, Object> maps;




    private Properties pros;




  



    //...



}





  配置文件:








<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 

                                                    

                                                        
                                                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
                                                    

                                                    

                                                      

                                                    

                                                        
                                                        <bean 
                                                            id="CustomerBean" class="com.mkyong.common.Customer">
                                                    

                                                    

                                                      

                                                    

                                                            
                                                        <!-- java.util.List -->
                                                    

                                                    

                                                            
                                                        <property 
                                                            name="lists">
                                                    

                                                    

                                                                
                                                        <list
                                                            >
                                                    

                                                    

                                                                    
                                                        <value
                                                            >1</value
                                                                >
                                                    

                                                    

                                                                    
                                                        <ref 
                                                            bean="PersonBean" />
                                                    

                                                    

                                                                    
                                                        <bean 
                                                            class="com.mkyong.common.Person">
                                                    

                                                    

                                                                        
                                                        <property 
                                                            name="name" value="mkyongList" />
                                                    

                                                    

                                                                        
                                                        <property 
                                                            name="address" value="address" />
                                                    

                                                    

                                                                        
                                                        <property 
                                                            name="age" value="28" />
                                                    

                                                    

                                                                    
                                                        </bean
                                                            >
                                                    

                                                    

                                                                
                                                        </list
                                                            >
                                                    

                                                    

                                                            
                                                        </property
                                                            >
                                                    

                                                    

                                                      

                                                    

                                                            
                                                        <!-- java.util.Set -->
                                                    

                                                    

                                                            
                                                        <property 
                                                            name="sets">
                                                    

                                                    

                                                                
                                                        <set
                                                            >
                                                    

                                                    

                                                                    
                                                        <value
                                                            >1</value
                                                                >
                                                    

                                                    

                                                                    
                                                        <ref 
                                                            bean="PersonBean" />
                                                    

                                                    

                                                                    
                                                        <bean 
                                                            class="com.mkyong.common.Person">
                                                    

                                                    

                                                                        
                                                        <property 
                                                            name="name" value="mkyongSet" />
                                                    

                                                    

                                                                        
                                                        <property 
                                                            name="address" value="address" />
                                                    

                                                    

                                                                        
                                                        <property 
                                                            name="age" value="28" />
                                                    

                                                    

                                                                    
                                                        </bean
                                                            >
                                                    

                                                    

                                                                
                                                        </set
                                                            >
                                                    

                                                    

                                                            
                                                        </property
                                                            >
                                                    

                                                    

                                                      

                                                    

                                                            
                                                        <!-- java.util.Map -->
                                                    

                                                    

                                                            
                                                        <property 
                                                            name="maps">
                                                    

                                                    

                                                                
                                                        <map
                                                            >
                                                    

                                                    

                                                                    
                                                        <entry 
                                                            key="Key 1" value="1" />
                                                    

                                                    

                                                                    
                                                        <entry 
                                                            key="Key 2" value-ref="PersonBean" />
                                                    

                                                    

                                                                    
                                                        <entry 
                                                            key="Key 3">
                                                    

                                                    

                                                                        
                                                        <bean 
                                                            class="com.mkyong.common.Person">
                                                    

                                                    

                                                                            
                                                        <property 
                                                            name="name" value="mkyongMap" />
                                                    

                                                    

                                                                            
                                                        <property 
                                                            name="address" value="address" />
                                                    

                                                    

                                                                            
                                                        <property 
                                                            name="age" value="28" />
                                                    

                                                    

                                                                        
                                                        </bean
                                                            >
                                                    

                                                    

                                                                    
                                                        </entry
                                                            >
                                                    

                                                    

                                                                
                                                        </map
                                                            >
                                                    

                                                    

                                                            
                                                        </property
                                                            >
                                                    

                                                    

                                                      

                                                    

                                                            
                                                        <!-- java.util.Properties -->
                                                    

                                                    

                                                            
                                                        <property 
                                                            name="pros">
                                                    

                                                    

                                                                
                                                        <props
                                                            >
                                                    

                                                    

                                                                    
                                                        <prop 
                                                            key="admin">admin@nospam.com</prop
                                                                >
                                                    

                                                    

                                                                    
                                                        <prop 
                                                            key="support">support@nospam.com</prop
                                                                >
                                                    

                                                    

                                                                
                                                        </props
                                                            >
                                                    

                                                    

                                                            
                                                        </property
                                                            >
                                                    

                                                    

                                                      

                                                    

                                                        
                                                        </bean
                                                            >
                                                    

                                                    

                                                      

                                                    

                                                        
                                                        <bean 
                                                            id="PersonBean" class="com.mkyong.common.Person">
                                                    

                                                    

                                                            
                                                        <property 
                                                            name="name" value="mkyong1" />
                                                    

                                                    

                                                            
                                                        <property 
                                                            name="address" value="address 1" />
                                                    

                                                    

                                                            
                                                        <property 
                                                            name="age" value="28" />
                                                    

                                                    

                                                        
                                                        </bean
                                                            >
                                                    

                                                    

                                                      

                                                    

                                                    </
                                                        beans>                                                            

                                                

                                                

                                        

运行:


                                            

                                                                

                                                                    

                                                                    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);
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        }
                                                                    

                                                                    
}

                                                                

                                                            

                                        

  我们也可以使用ListFactoryBean。The ‘ListFactoryBean‘ class provides developer a way to create a concrete List collection class (ArrayList and LinkedList) in Spring’s bean configuration file.


                                            

                                                                

                                                                    

                                                                    <
                                                                        bean id="CustomerBean" class="com.mkyong.common.Customer">                                                                            

                                                                    

                                                                            
                                                                        <property 
                                                                            name="lists">
                                                                    

                                                                    

                                                                                
                                                                        <bean 
                                                                            class="org.springframework.beans.factory.config.ListFactoryBean">
                                                                    

                                                                    

                                                                                    
                                                                        <property 
                                                                            name="targetListClass">
                                                                    

                                                                    

                                                                                        
                                                                        <value
                                                                            >java.util.ArrayList</value
                                                                                >
                                                                    

                                                                    

                                                                                    
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                                    
                                                                        <property 
                                                                            name="sourceList">
                                                                    

                                                                    

                                                                                        
                                                                        <list
                                                                            >
                                                                    

                                                                    

                                                                                            
                                                                        <value
                                                                            >1</value
                                                                                >
                                                                    

                                                                    

                                                                                            
                                                                        <value
                                                                            >2</value
                                                                                >
                                                                    

                                                                    

                                                                                            
                                                                        <value
                                                                            >3</value
                                                                                >
                                                                    

                                                                    

                                                                                        
                                                                        </list
                                                                            >
                                                                    

                                                                    

                                                                                    
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                                
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                

                                                            

                                        

  或者:加入:xmlns:util="http://www.springframework.org/schema/util"然后就可以:


                                            

                                                                

                                                                    

                                                                    <
                                                                        beans xmlns="http://www.springframework.org/schema/beans"                                                                            

                                                                    

                                                                        
                                                                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                                                    

                                                                    

                                                                        
                                                                        xmlns:util="http://www.springframework.org/schema/util"
                                                                    

                                                                    

                                                                        
                                                                        xsi:schemaLocation="http://www.springframework.org/schema/beans
                                                                    

                                                                    

                                                                        
                                                                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                                                                    

                                                                    

                                                                        
                                                                        http://www.springframework.org/schema/util
                                                                    

                                                                    

                                                                        
                                                                        http://www.springframework.org/schema/util/spring-util-2.5.xsd">
                                                                    

                                                                    

                                                                      

                                                                    

                                                                        
                                                                        <bean 
                                                                            id="CustomerBean" class="com.mkyong.common.Customer">
                                                                    

                                                                    

                                                                            
                                                                        <property 
                                                                            name="lists">
                                                                    

                                                                    

                                                                                
                                                                        <util:list 
                                                                            list-class="java.util.ArrayList">
                                                                    

                                                                    

                                                                                    
                                                                        <value
                                                                            >1</value
                                                                                >
                                                                    

                                                                    

                                                                                    
                                                                        <value
                                                                            >2</value
                                                                                >
                                                                    

                                                                    

                                                                                    
                                                                        <value
                                                                            >3</value
                                                                                >
                                                                    

                                                                    

                                                                                
                                                                        </util:list
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                    

                                                                      

                                                                    

                                                                    </
                                                                        beans>                                                                            

                                                                

                                                            

                                        

 

         SetFactoryBean The ‘SetFactoryBean‘ class provides developer a way to create a concrete Set collection (HashSet and TreeSet) in Spring’s bean configuration file.

set和上面的一样:


                                            

                                                                

                                                                    

                                                                    <
                                                                        bean id="CustomerBean" class="com.mkyong.common.Customer">                                                                            

                                                                    

                                                                            
                                                                        <property 
                                                                            name="sets">
                                                                    

                                                                    

                                                                                
                                                                        <util:set 
                                                                            set-class="java.util.HashSet">
                                                                    

                                                                    

                                                                                    
                                                                        <value
                                                                            >1</value
                                                                                >
                                                                    

                                                                    

                                                                                    
                                                                        <value
                                                                            >2</value
                                                                                >
                                                                    

                                                                    

                                                                                    
                                                                        <value
                                                                            >3</value
                                                                                >
                                                                    

                                                                    

                                                                                
                                                                        </util:set
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                

                                                            

                                        

         MapFactoryBean The ‘MapFactoryBean‘ class provides developer a way to create a concrete Map collection class (HashMap and TreeMap) in Spring’s bean configuration file.

map也一样:


                                            

                                                                

                                                                    

                                                                    <
                                                                        bean id="CustomerBean" class="com.mkyong.common.Customer">                                                                            

                                                                    

                                                                            
                                                                        <property 
                                                                            name="maps">
                                                                    

                                                                    

                                                                                
                                                                        <util:map 
                                                                            map-class="java.util.HashMap">
                                                                    

                                                                    

                                                                                    
                                                                        <entry 
                                                                            key="Key1" value="1" />
                                                                    

                                                                    

                                                                                    
                                                                        <entry 
                                                                            key="Key2" value="2" />
                                                                    

                                                                    

                                                                                    
                                                                        <entry 
                                                                            key="Key3" value="3" />
                                                                    

                                                                    

                                                                                
                                                                        </util:map
                                                                            >
                                                                    

                                                                    

                                                                            
                                                                        </property
                                                                            >
                                                                    

                                                                    

                                                                        
                                                                        </bean
                                                                            >
                                                                    

                                                                

                                                            

                                        

  

 

 

 

 


============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835122.html,如需转载请自行联系原作者
版权声明:本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

用云栖社区APP,舒服~

【云栖快讯】中办国办印发《推进互联网协议第六版(IPv6)规模部署行动计划》加快推进基于 IPv6 的下一代互联网规模部署,计划指出2025年末中国 IPv6 规模要达到世界第一,阿里云也第一时间宣布了将全面提供IPv6服务,那么在全面部署 IPV6 前,你需要了解都在这儿  详情请点击 评论文章 (0) (0) (0)
分享到:

相关文章

网友评论