We mentioned spring Bean factory when talking about the factory model. Here is a detailed description:
Http://wenku.baidu.com/view/69f94e80d0d233d4b14e6970.html? St = 1 download in Word format: Download in Word format:
Spring bean containers and bean factories, also known as IOC (inverseof control), are interface-oriented and abstract-oriented.
Getting started with spring:
The moveable interface is also available:
PackageCom. bjsxt. Spring. factory;
PublicInterfaceMoveable {
VoidRun ();
}
Of course, there are also implementations.MoveableSubclass of the interface:
CarAndTrain
PackageCom. bjsxt. Spring. factory;
PublicClassCarImplementsMoveable {
PublicVoidRun (){
System.Out. Println ("Running in smokeCar .......");
}
}
PackageCom. bjsxt. Spring. factory;
PublicClassTrainImplementsMoveable {
@ Override
PublicVoidRun (){
System.Out. Println ("Whining");
}
}
The key is in test (User:
PackageCom. bjsxt. Spring. factory;
ImportJava. Io. ioexception;
ImportOrg. springframework. Beans. Factory. beanfactory;
ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;
//We used to usePropertiesFile to read information,Obtain the name of the class. Then, we get the object of this class through reflection.
PublicClassTest {
/**
*@ ParamARGs
*@ ThrowsIoexception
*/
PublicStaticVoidMain (string [] ARGs)Throws
Exception {
//PassXMLFile to read information
//BecauseXMLTherefore, the reading method is different.
Beanfactory F =NewClasspathxmlapplicationcontext ("Applicationcontext. xml");
Object o = f. getbean ("V");
Moveable M = (moveable) O;
M. Run ();
}
}
Let's take a look at the information in the XML file:
<?XMLVersion="1.0"Encoding=UTF-8"?>
<BeansXmlns=Http://www.springframework.org/schema/beans"
Xmlns: xsi=Http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation=Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<BeanID="V"Class="Com. bjsxt. Spring. Factory. Train">
</Bean>
<! --// V = com. bjsxt. Spring. Factory. Car -->
</Beans>
It's easy.Bean, ItsIDIsVAnd then itsClassYesCom. bjsxt. Spring. Factory. Train
Download the Word document in this article:
Http://wenku.baidu.com/view/69f94e80d0d233d4b14e6970.html? St = 1