Beans, Bean Factory FlApplicationContext this. beanTwo yetAnotherBean this. i 正如你所看到的,bean定义中指定的构造函数参数将会作为 ExampleBean的构造函数参数被传入。 现在考虑一下不用构造函数,而是调用一个静态工厂方法来返回一个对象的实例: <bean id="example Bean class="examples. ExampleBean factory-method=createInstance> <constructor-argXref bean="anotherExampleBean"/>/constructor-arg) <constructor-arg><ref bean="yetAnother"/></constructor-arg> <constructor-arg><value>1</value></constructor-arg) </bean> <bean id="anotherExampleBean" class="examples. AnotherBean/> <bean id="yetAnotherBean" class="examples. YetAnotherBean/> public class ExampleBean I // a private constructor private // a static factory method // the arguments to this method can be considered the dependencies of the bean that // is returned, regardless of how those arguments are actually used. public static ExampleBean ExampleBean (AnotherBean anotherBean, YetAnotherBean yetAnother Bean, int i) ExampleBean eb new ExampleBean (.) // some other operations return eb 需要注意的是:静态工厂方法的参数由 constructor-arg元素提供,这和构造函数的用法是一样的。这些 参数是可选的。重要的一点是工厂方法所返回的对象类型不一定和包含这个静态工厂方法的类一致, 虽然上面这个例子中是一样的。前面所提到的实例工厂方法(non- static)用法基本上是一样的(除 了使用 factory-bean属性代替 class属性),在这里就不再详细叙述了。 3.3.2.深入Bean属性和构造函数参数 正如前面提到的那样,bean的属性和构造函数参数可以被定义为其他bean的引用(合作者),或者内 联定义的值。为了达到这个目的, XmlBeanFactory在 property和 constructor-arg元素中支持许多子元素类型 value元素用适合人读的字符串形式指定属性或构造函数参数。正如前面提到的那样, JavaBeans的 Property Editors被用来将这些字符串从java.lang. String类型转变为真实的属性类型或参数类型。 <beans> <bean id="myDataSource class="org. apache commons. dbcp. BasicDataSource destroy-method=close> <I- results in a setDriverClass Name(String) call -> property name=driver ClassName"> Version 1.1
+2'9$%&Ý ɦ -$+/&+$,#$%&ɧ +2'2 ɦ 2ɧ ɩ ɩ ȾĤĄŖʶŊ:I9$%&ablja:VWXYZYǦəK3%B01$#$%&:VWXYZYß!Ƙ Ô¡źwŗzVWXYIy{¤wxɳŦ[\¨¥¦wx^_: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