Spring instantiates Bean using the static factory Method

Source: Internet
Author: User

Spring instantiates Bean using the static factory Method

Knowledge Point introduction:
Static factory, as its name implies, is to call the static factory method to obtain the desired objects. To allow spring to manage all objects, we cannot directly use engineering. the static method () is used to obtain objects, but is still obtained through spring injection.
[For more information, see http://blog.csdn.net/mahoking]

Procedure:
1. Create a Speaker object.

/*** Static factory class **/public class Speaker {private String name; private String topic; private int timeHour; private static Speaker speaker = null; private Speaker () {this. name = Jacke; this. topic = play football !; This. timeHour = 2;}/*** factory Method * @ return */public static Speaker getInstance () {if (null = speaker) {speaker = new Speaker ();} return speaker;}/*** speech */public void speech () {System. out. println (toString () ;}@ Overridepublic String toString () {return Speaker [name = + name +, topic = + topic +, timeHour = + timeHour +] ;}}


2. Create the Spring configuration file beanLearn03.xml.

 
 
  
   
  
 

 

3. Introduce the Spring configuration file beanLearn03.xml into the main configuration file beans. xml.

 
 

 

4. Compile the test class TestSpring03.java.

Public class TestSpring03 {public static void main (String [] args) {BeanFactory beanFactory = new ClassPathXmlApplicationContext (beans. xml); // use the static factory method to instantiate BeanSpeaker speaker03 = beanFactory. getBean (speaker03, Speaker. class); speaker03.speech ();}}

 

 

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.