承接上文
引用其他对象或类型的成员
.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>
- StaticProperty的值必须填完整
- 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>
- 字段必须有get和set的方法
java和Csharp的共同点
- theadd1是静态方法,theadd2为实例方法
- 就是标签的name几乎一样
- 分别的效果是获取属性、静态字段(csharp为静态属性)、获取字段、静态方法返回、实例方法返回