zoukankan      html  css  js  c++  java
  • spring List,Set,Map,Properties,array的使用配置文件注入实例

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
      
    <bean id="chinese" class="Bean.collections.Chinese">
        
    <!--List 注入例子-->
        
    <property name="schools">    
          
    <list>
             
    <value>小学</value>
             
    <value>中学</value>
             
    <value>大学</value>
          
    </list>
        
    </property>
        
    <!--Properties 注入例子-->
        
    <property name="health">
           
    <props>
             
    <prop key="血压">正常</prop>
             
    <prop key="身高">178</prop>
           
    </props>
        
    </property>
        
    <!--Map 注入例子-->
        
    <property name="scores">
          
    <map>
            
    <entry key="数学">
               
    <value>88</value>
            
    </entry>
            
    <entry key="语文">
               
    <value>99</value>
            
    </entry>
          
    </map>
        
    </property>
     
    <!--Map 例子-->
     <bean id="accountConfig" class="java.util.HashMap">
      <constructor-arg>
       <map>
        <entry key="accountResourceSQL">
         <value>SELECT * FROM ABC</value>
        </entry>
       </map>
      </constructor-arg>
     </bean>
     
        <!-Set 注入例子-->
        
    <property name="axes">
          
    <set>
            
    <value>字符串斧子</value>
            
    <!-- 用嵌套bean定义属性 -->
            
    <bean class="Bean.collections.WoodAxe"/>
            
    <!-- 引用bean作为属性 -->
            
    <ref local="steelaxe"/>
          
    </set>
        
    </property>
      <!--array 注入例子-->
      <property name="array">  
       <list>  
        <value>array1</value>  
        <value>array2</value>  
       </list>  
      </property> 
      
    </bean>
      
    <bean id="steelaxe" class="Bean.collections.SteelAxe"></bean>
    </beans>


    实例java代码:

    package Bean.collections;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    import Bean.collections.Person;
    public class Chinese implements Person {
        
    private List schools=new ArrayList();
        
    private Map scores=new HashMap();
        
    private Properties health=new Properties();
        
    private Set axes=new HashSet();
        
    public Set getAxes() {
            
    return axes;
        }
        
    public void setAxes(Set axes) {
            
    this.axes = axes;
        }
        
    public Properties getHealth() {
            
    return health;
        }
        
    public void setHealth(Properties health) {
            
    this.health = health;
        }
        
    public List getSchools() {
            
    return schools;
        }
        
    public void setSchools(List schools) {
            
    this.schools = schools;
        }
        
    public Map getScores() {
            
    return scores;
        }
        
    public void setScores(Map scores) {
            
    this.scores = scores;
        }
        
    public void useAxe() {
            System.out.println(schools);
            System.out.println(scores);
            System.out.println(axes);
            System.out.println(health);
        }

    }


    jdbc.properties配置文件实例:

    /WEB-INF/jdbc.properties

    jdbc.driver=org.postgresql.Driver   
    jdbc.url=jdbc:postgresql://localhost/test   
    jdbc.user=postgres   
    jdbc.password=  

    Bean配置如下:

    <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
        
    <property name="location">   
            
    <value>/WEB-INF/jdbc.properties</value>   
        
    </property>   
    </bean> 

    或者使用多个配置文件:

    <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     
        
    <property name="locations">      
             
    <list>       
               
    <value>/WEB-INF/jdbc.properties</value>      
            
    </list>     
        
    </property>    
    </bean> 

    applicationContext.xml中数据源配置:

    <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
        
    <property name="driverClassName">   
            
    <value>${jdbc.driver}</value>   
        
    </property>   
        
    <property name="url">   
            
    <value>${jdbc.url}</value>   
        
    </property>   
        
    <property name="username">   
            
    <value>${jdbc.user}</value>   
        
    </property>   
        
    <property name="password">   
            
    <value>${jdbc.password}</value>   
        
    </property>   
    </bean>  
  • 相关阅读:
    autorelease的对象何时被释放
    如何处理webView跳转
    根据Url 获取图片尺寸 iOS
    iOS开发证书"此证书的签发者无效"解决方法
    IOS, xib和storyboard的混用
    友盟社交分享中的那些坑
    iOS手势(滑动)返回的实现(自定义返回按钮)
    关于tableview下拉刷新崩溃的问题
    dispatch_async 和dispatch_sync
    ios调用系统界面显示英文
  • 原文地址:https://www.cnblogs.com/jifeng/p/2130497.html
Copyright © 2011-2022 走看看