Spring uses the instance factory method to instantiate Bean
Knowledge Point introduction:
The instance factory means that the method for obtaining the object instance is not static, so you need to first create a new factory class and then call a common instance method.
[For more information, see http://blog.csdn.net/mahoking]
Procedure:
1. Create a Speaker object.
Public class Speaker {// use the instance factory method to instantiate Beanprivate String speakerName; public Speaker (String speakerName) {this. speakerName = speakerName;}/*** speech */public void speech () {System. out. println (toString () ;}@ Overridepublic String toString () {return Speaker [speakerName = + speakerName +] ;}}
2. Create a SpeakerFactory object.
Public class SpeakerFactory {/*** factory Method * @ param speakerName * @ return */public Speaker newInstance () {String speakerName = Lucy And Lily; return new Speaker (speakerName );}}
3. Create the Spring configuration file beanLearn04.xml.
4. Introduce the Spring configuration file beanLearn04.xml to the main configuration file beans. xml.
5. Compile the test class TestSpring04.java.
Public class TestSpring04 {public static void main (String [] args) {BeanFactory beanFactory = new ClassPathXmlApplicationContext (beans. xml); // use the instance factory method to instantiate BeanSpeaker speaker04 = beanFactory. getBean (speaker04, Speaker. class); speaker04.speech ();}}