做系統(tǒng)網(wǎng)站建設外貿(mào)網(wǎng)站推廣費用
1.首先說說?factory-method 是指定創(chuàng)造實例的工廠方法,用法:
????????factory-method 和 class 配合使用,這時?factory-method 必須是class所指定的類中的一個靜態(tài)方法,也就是Spring會直接調(diào)用 class 所指定的類的靜態(tài)工廠方法創(chuàng)建一個實例,然后注冊到IOC 容器中
????????factory-method 和 factory-bean 配合使用,factory-bean必須是IOC容器中存在的一個bean實例? beanA,factory-method 配置?factory-bean 所指定的實例?beanA?的一個工廠方法,也就是Spring會先創(chuàng)建?factory-bean 所指定的這個實例 beanA,然后調(diào)用beanA的工廠方法,創(chuàng)建一個新的實例 beanB,然后注冊到IOC 容器中
以上描述可以看出?
? ? ? ? 1.factory-bean 這個屬性和 接口 FactoryBean 沒有關系
? ? ? ? 2.factory-method 必須要和 class 或者?factory-bean 中的一個配合使用,如果class 和?factory-bean都配置了,那么class就不起作用了
factory-bean的官方說明: Alternative to class attribute for factory-method usage.If this is specified, no class attribute should be used.This must be set to the name of a bean in the current or ancestor factories that contains the relevant factory method.This allows the factory itself to be configured using Dependency Injection, and an instance (rather than static) method to be used. 翻譯:使用factory-method時,class屬性的替代方案,如果指定了這個(factory-bean),class屬性就不用了。factory-bean必須設置一個在當前容器或者父容器中存在的bean,并且這個bean必須擁有factory-method所指定的工廠方法。這種配置方式容許這個工廠bean通過依賴注入進行配置,factory-method 配置的是一個實例方法,不是靜態(tài)方法
<bean id="exampleFactory" class="com.example.ExampleFactory"><!-- Inject dependencies here -->
</bean><bean id="exampleBean" factory-bean="exampleFactory" factory-method="createInstance"/>
factory-method的官方說明: The name of a factory method to use to create this object. Use constructor-arg elements to specify arguments to the factory method,if it takes arguments. Autowiring does not apply to factory methods. 創(chuàng)建這個對象的工廠方法的方法名,如果工廠方法需要參數(shù),就用constructor-arg標簽來給工廠方法指定參數(shù)。自動注入不會被應用到工廠方法。If the "class" attribute is present, the factory method will be a static method on the class specified by the "class" attribute on this bean definition. Often this will be the same class as that of the constructed object - for example, when the factory method is used as an alternative to a constructor. However, it may be on a different class. In that case, the created object will *not* be of the class specified in the "class" attribute. This is analogous to FactoryBean behavior. 如果配置了class屬性,factory-method必須得是這個bean definition的class屬性指定的類中的一個靜態(tài)方法,通常這個class屬性指定的類和工廠方法創(chuàng)建的對象所屬的類是相同的,舉例:當factory method被當作constructor的替代方案來創(chuàng)造對象時,這個factory method可能屬于一個不同的類。這種情況下factory method創(chuàng)造的對象就不是class屬性所指定的類的實例。這有點類似FactoryBean的用法。If the "factory-bean" attribute is present, the "class" attribute is not used, and the factory method will be an instance method on the object returned from a getBean call with the specified bean name. The factory bean may be defined as a singleton or a prototype. 如果指定了factory-bean這個屬性,class屬性就不會啟用了,factory-method必須是,用factory-bean屬性作為參數(shù),調(diào)用getBean返回的實例對象的一個實例方法(必須是 getBean(factory-bean)的一個實例方法 )。factory-bean 可以是單例模式,也可以是原型模式The factory method can have any number of arguments. Autowiring is not supported. Use indexed constructor-arg elements in conjunction with the factory-method attribute. factory-method 可以有任意個數(shù)的參數(shù),不支持工廠方法自動注入。把排好順序的constructor-arg和factory-method結合使用。Setter Injection can be used in conjunction with a factory method.Method Injection cannot, as the factory method returns an instance,which will be used when the container creates the bean. set方法注入可以和工廠方法聯(lián)合使用,方法注入不可以和工廠方法不可以聯(lián)合使用,因為當容器需要創(chuàng)造bean的時候,工廠方法返回一個實例。
????????