Ⅲ.spring的点点滴滴--赋值

承接上文

对象的赋值(调用方式都一样不再阐述)


.net篇(环境为vs2012+Spring.Core.dll v1.31

public class PropertyDemo{
   public System.Collections.ArrayList PropertyList { get; set; }
   public System.Collections.Specialized.HybridDictionary PropertyDictionary { get; set; }
   public System.Collections.Specialized.NameValueCollection PropertyNameValue { get; set; }
   public Spring.Collections.Set PropertySet { get; set; }
   public System.Collections.Generic.List<string> PropertyStrList { get; set; }
   public System.Collections.Generic.Dictionary<string, string> PropertyStrDictionary { get; set; }
   public string this[int index]  {
      get { return (string)PropertyList[index]; }
      set { PropertyList[index] = value; }
   } 
   public string this[string keyName] {
      get { return (string)PropertyDictionary[keyName]; }
      set { PropertyDictionary.Add(keyName, value); } 
   }
   public DateTime PropertyTime { get; set; }
}
 <object name="PropertyDemo" type="SpringBase.PropertyDemo,SpringBase">
    <property name="PropertyList">
      <list>
        <value>a list element followed by a reference</value>
      </list>
    </property>
    <property name="PropertyDictionary">
      <dictionary>
        <entry key="a string => string entry" value="just some string"/>
      </dictionary>
    </property>
    <property name="PropertyNameValue">
      <name-values>
        <add key="HarryPotter" value="The magic property"/>
        <add key="JerrySeinfeld" value="The funny (to Americans) property"/>
      </name-values>
    </property>
    <property name="PropertySet">
      <set>
        <value>just some string</value>
      </set>
    </property>
    <property name="PropertyStrList">
      <list element-type="string">
        <value>a</value>
        <value>b</value>
        <value>c</value>
      </list>
    </property>
    <property name="PropertyStrDictionary">
      <dictionary key-type="string"  value-type="string">
        <entry key="zero">
          <value>jerry</value>
        </entry>
        <entry key="one">
          <value>Month</value>
        </entry>
        <entry key="two">
          <value>Nikola Tesla</value>
        </entry>
        <entry key="three">
          <value>DateTime.Today</value>
        </entry>
      </dictionary>
    </property>
    <property name="[0]" value="Master Shake"/> 
    <property name="['one']" value="uno"/>
    <property name="PropertyTime" expression="DateTime.Today + 7"/>
  </object>

上面的标签和csharp的类型需要对应或者用基类也可以, expression是表达式运行的时候会自动换行为响应的值,key-type设置是泛型属性key的类型, key-value设置是泛型属性值的类型,属性索引老版本可能不是这样的设置的


java篇(环境为Maven+Jdk1.7+IntelliJ IDEA 12.1.4

package springdemo;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
public class PropertyDemo {
    private ArrayList propertyList;
    public ArrayList getPropertyList() {
        return propertyList;
    }
    public void setPropertyList(ArrayList propertyList) {
        this.propertyList = propertyList;
    }
    private Map propertyDictionary;
    public Map getPropertyDictionary() {
        return propertyDictionary;
    }
    public void setPropertyDictionary(Map propertyDictionary) {
        this.propertyDictionary = propertyDictionary;
    }
    private Set propertySet;
    public Set getPropertySet() {
        return propertySet;
    }
    public void setPropertySet(Set propertySet) {
        this.propertySet = propertySet;
    }
    private Properties  propertyprops;
    public Properties getPropertyprops() {
        return propertyprops;
    }
    public void setPropertyprops(Properties propertyprops) {
        this.propertyprops = propertyprops;
    }
}
 <bean id="PropertyDemo" class="springdemo.PropertyDemo">
        <property name="propertyList">
            <list>
                <value>a list element followed by a reference</value>
            </list>
        </property>
        <property name="propertyDictionary">
            <map>
                <entry key="a string => string entry" value="just some string"/>
            </map>
        </property>
        <property name="propertySet">
            <set>
                <value>just some string</value>
            </set>
        </property>
        <property name="propertyprops">
            <props>
                <prop key="db">172.0.0.1</prop>
                <prop key="name">myself</prop>
            </props>
        </property>
 </bean>

不知道java东西少还是什么只有这些,因为java是伪泛型也不支持属性索引器, 因为没有文档都是参考c# 探索出java


javaCsharp的共同点

其中我们在标签里面还可以用ref | idref标签或者属性来通过id | name引用其他的对象, 许多标签的属性可以当作一个独立的标签,如下

<entry key="myKey"> <value>hello</value></entry>

java中必须要有set字段的方法,Csharp可以省略


posted @ 2013-12-05 13:47  cnljli  阅读(527)  评论(0编辑  收藏  举报