Spring instantiation with a factory method

Source: Internet
Author: User

Spring Init A Bean with Factory


1. The static factory method gets the bean

<bean id= "ClientService" class= "examples. ClientService "factory-method=" CreateInstance "/>
public class ClientService {private static ClientService ClientService = new ClientService ();            Private ClientService () {} public static ClientService CreateInstance () {return clientservice; }        }


2, factory instance non-static method

<!-- the factory bean, which contains a method called  CreateInstance ()  --><bean id= "Servicelocator"  class= "examples. Defaultservicelocator "><!-- inject any dependencies required by this  locator bean --></bean><!-- the bean to be created via  the factory bean --><bean id= "ClientService"  factory-bean= "ServiceLocator " factory-method=" createclientserviceinstance "/><bean id=" ClientService2 " factory-bean=" Servicelocator " factory-method=" CreateClientServiceInstance2 "/>public class  Defaultservicelocator {    private static clientservice clientservice  = new clientserviceimpl ();        private  Defaultservicelocator ()  {}    public clientsErvice createclientserviceinstance ()  {        return  clientservice;    }    public  clientservice  CreateClientServiceInstance2 ()  {        return clientservice ;     }}


A factory's method of getting a bean can have multiple



Spring instantiation with a factory method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.