What is the difference between spring factory-method and factory-bean?

前端 未结 6 2135
小鲜肉
小鲜肉 2021-02-02 10:28

I am new Springs. In Bean tag I found factory-method and factory-bean Attributes. What is the difference between factory-method and factory-bean?

I am using factory-meth

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 10:52

    factory-method:
    As everyone has already explained, factory-method is used to tell the spring to use the factory method to instantiate the object.

    Note: static method is mandatory else throws an exception.

    
    

    factory-bean:
    When there is no static method and still you want to create the object using non static method, here is the trick can be done using factory-bean:

    
    

    Note: no class attribute required in the above spring definition.

    Class:

    public class ServiceFactory {
    
        public static ServiceFactory getServiceFacrotyWithStaticMethod(){
            return new ServiceFactory();
        }
    
        public SdCard getInstanceWithOutStaticMethod(){
            return new AdaptorSlot("laptop");
        }
    }
    

提交回复
热议问题