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 ();}}