Spring (3.2.3), spring3.2.3

Source: Internet
Author: User

Spring (3.2.3), spring3.2.3

To create a Bean instance object, follow these steps:

  • Call the constructor to create a Bean instance
  • Call the static factory method to create a Bean instance
  • Call the instance factory method to create a Bean instance

 

Use the constructor to create a Bean instance

XML configuration:

<bean id="exampleBean" class="examples.ExampleBean"/><bean name="anotherExample" class="examples.ExampleBeanTwo"/>

 

Create Bean instance using static factory Method

XML configuration:

<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/>

Static factory class:

public class ClientService {    private static ClientService clientService = new ClientService();    private ClientService() {}    public static ClientService createInstance() {        return clientService;    }}

 

Create a Bean instance using the instance factory Method

XML configuration:

<!-- 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"/>

Instance factory type:

public class DefaultServiceLocator {    private static ClientService clientService = new ClientServiceImpl();    private DefaultServiceLocator() {}    public ClientService createClientServiceInstance() {        return clientService;    }}

 

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.