By default, spring containers parse the configuration file and instantiate all classes in the file when the service starts.
When we use spring directly, we obtain the bean injected by spring,
Applicationcontext CTX =NewClasspathxmlapplicationcontext ("Spring. xml ");
Myservice myservice1 = (myservice) CTX. getbean ("myservice ");
Next we will simulate the bean management process in spring. The Code is as follows:
1. Step 1: Create a Java project and introduce spring. Jar
2. Create a spring. xml configuration file
<? XML version ="1.0"Encoding =UTF-8"?>
<Beans xmlns =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-2.5.xsd">
</Beans>
3. Create the interface myservice. Only one test method is required. Save
4. Create an implementation class myserviceimpl. The console outputs a sentence.
5. Create a parsing class named myclasspathxmlapplicationcontext
Mainly two steps in the constructor
// Load the instantiated Bean
PrivateMap <string, Object> beanmap =
NewHashmap <string, Object> ();
// Load the attributes and values of the configuration file
PrivateList <mybeans> beanlist =
NewArraylist <mybeans> ();
PublicMyclasspathxmlapplicationcontext (string filename ){
// Step 1: parse the spring configuration file
Readxml (filename );
// Step 2: instantiate all injected beans through reflection
Initbeans ();
}
/**
* Initialize the bean in the configuration file through the reflection mechanism
*/
Private voidInitbeans (){
For(Mybeans Bean: beanlist ){
Try{
If(Bean. getclassname ()! =
Null&&! "". Equals (bean. getclassname ())){
Beanmap. Put (bean. GETID (), class.Forname(Bean. getclassname (). newinstance ());
}
}Catch(Exception e ){
E. printstacktrace ();
}
}
}
/**
* Parse the configuration file, set the parsed bean to the object, and keep it to list
*
*@ ParamFilename
*/
Private voidReadxml (string filename ){
Saxreader reader =NewSaxreader ();
Document Doc =Null;
URL xmlpath =This. Getclass (). getclassloader (). getresource (filename );
Try{
Map <string, string> nsmap =NewHashmap <string, string> ();
Nsmap. Put ("ns", "http://www.springframework.org/schema/beans ");
Doc = reader. Read (xmlpath );
XPath = Doc. createxpath ("// ns: Beans // ns: Bean"); // create // ns: Beans // ns: bean query path
XPath. setnamespaceuris (nsmap); // you can specify a namespace.
List <element> eles =XPath. selectnodes (DOC); // Retrieve all nodes under the document
For(Element: eles ){
String id = element. attributevalue ("ID ");
String Cn = element. attributevalue ("class ");
// Customize the object bean and save the ID and class in the configuration file
Mybeans beans =NewMybeans (ID, CN );
Beanlist. Add (beans );
}
}Catch(Exception e ){
E. printstacktrace ();
}
}
PublicObject getbean (string beanid ){
ReturnBeanmap. Get (beanid );
}
6. entity class
PackageCom. mooing. Service;
Public classMybeans {
PrivateString ID;
PrivateString classname;
PublicMybeans (string ID, string classname ){
This. ID = ID;
This. Classname = classname;
}
PublicString GETID (){
ReturnID;
}
Public voidSetid (string ID ){
This. ID = ID;
}
PublicString getclassname (){
ReturnClassname;
}
Public voidSetclassname (string classname ){
This. Classname = classname;
}
}
7. Test
Myclasspathxmlapplicationcontext CTX =NewMyclasspathxmlapplicationcontext ("Spring. xml ");
Myservice = (myservice) CTX. getbean ("myservice ");
Myservice. Save ();
Summary:
Custom Code can also be instantiated using spring containers. That is to say, the actual spring instantiation management bean also takes two major steps: first, the service starts parsing the configuration file, and save the elements in the configuration file. Second, instantiate all elements and provide methods for getting instances.