Ⅴ.spring的點點滴滴--引用其餘對象或類型的成員

承接上文

引用其餘對象或類型的成員


.net篇(環境爲vs2012+Spring.Core.dll v1.31

public class Person {
    public string Name { get; set; }
    public static int Age { get; set; }
    public string sex;
    public static int Add(int x, int y){
        return x + y;
    }
    public int Add(int x, int y,int z){
        return x + y+z;
    }
}
<object id="person" type="SpringBase.Person,SpringBase">
    <property name="Name" value="cnljli" />
    <property name="Age" value="1"/>
    <property name="sex" value="0"/>
  </object>
  <object id="theName"
        type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
    <property name="TargetObject" ref="person"/>
    <property name="TargetProperty" value="Name"/>
  </object>
  <object id="theAge"
        type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
    <property name="StaticProperty" value="SpringBase.Person.Age"/>
  </object>
  <object id="thesex"
     type="Spring.Objects.Factory.Config.FieldRetrievingFactoryObject, Spring.Core">
    <property name="TargetObject" ref="person"/>
    <property name="TargetField" value="sex"/>
  </object>
  <object id="theadd1"
    type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
    <property name="TargetType"  value="SpringBase.Person,SpringBase"/>
    <property name="TargetMethod" value="Add"/>
    <property name="Arguments">
      <list>
        <value>1</value>
        <value>2</value>
      </list>
    </property>
  </object>
  <object id="theadd2"
    type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
    <property name="TargetObject"  ref="person"/>
    <property name="TargetMethod" value="Add"/>
    <property name="NamedArguments">
      <dictionary>
        <entry key="x" value="1" />
        <entry key="y" value="2" />
        <entry key="z" value="3" />
      </dictionary>
    </property>
  </object>
  1. StaticProperty的值必須填完整
  2. Arguments的值的時候是從上往下匹配,NamedArguments是經過鍵值對匹配

java篇(環境爲Maven+Jdk1.7+IntelliJ IDEA 12.1.4

package springdemo;
public class factoryObject {
    private String name;
    public static Integer age;
    public String sex;
    public static int Add(int x, int y) {
        return x + y;
    }
    public static Integer getAge() {
        return age;
    }
    public static void setAge(Integer age) {
        factoryObject.age = age;
    }
    public int Add(int x, int y, int z) {
        return x + y + z;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
<bean id="person" class="springdemo.factoryObject">
        <property name="name" value="cnljli"/>
        <property name="age" value="1"/>
        <property name="sex" value="0"/>
    </bean>
    <bean id="theName"
          class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
        <property name="targetBeanName" value="person"/>
        <property name="propertyPath" value="name"/>
    </bean>
    <bean id="theAge"
          class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <property name="staticField" value="springdemo.factoryObject.age"/>
    </bean>
    <bean id="thesex"
          class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <property name="TargetObject" ref="person"/>
        <property name="targetField" value="sex"/>
    </bean>
    <bean id="theadd1"
          class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="springdemo.factoryObject"/>
        <property name="targetMethod" value="Add"/>
        <property name="arguments">
            <list>
                <value>1</value>
                <value>2</value>
            </list>
        </property>
    </bean>
    <bean id="theadd2"
          class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="person"/>
        <property name="targetMethod" value="Add"/>
        <property name="arguments">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </list>
        </property>
    </bean>
  1. 字段必須有get和set的方法

javaCsharp的共同點 html

  1. theadd1是靜態方法,theadd2爲實例方法
  2. 就是標籤的name幾乎同樣
  3. 分別的效果是獲取屬性、靜態字段(csharp爲靜態屬性)、獲取字段、靜態方法返回、實例方法返回

相關文章
相關標籤/搜索