package com.fsti.factory.factorybean;public interface Output {public final static Integer MAX_CACHE_LINE = 3;void getData(String str);void out();}
Package COM. fsti. factory. factorybean; public class printer implements output {private string [] printdata = new string [max_cache_line]; // records the number of jobs to be printed. Private int datanum = 0; public void getdata (string Str) {If (datanum> = max_cache_line) {system. out. println ("failed to add when the output queue is full");} else {printdata [datanum ++] = STR ;}} public void out () {While (datanum> 0) {system. out. println ("printer printing:" + printdata [0]); // move the entire job to one place and reduce the number of remaining jobs to one system. arraycopy (printdata, 1, printdata, 0, -- datanum );}}}
Package COM. fsti. factory. factorybean; public class betterprinter implements output {private string [] printdata = new string [max_cache_line]; // records the number of jobs to be printed currently. Private int datanum = 0; public void getdata (string Str) {If (datanum> = max_cache_line) {system. out. println ("failed to add when the output queue is full");} else {printdata [datanum ++] = STR ;}} public void out () {While (datanum> 0) {system. out. println ("better printer printing:" + printdata [0]); // move the entire job to one place, and reduce the number of remaining jobs to one system. arraycopy (printdata, 1, printdata, 0, -- datanum );}}}
package com.fsti.factory.factorybean;public interface ApplicationContext {Object getBean(String name) throws Exception;}
Package COM. fsti. factory. factorybean; import Java. io. file; import Java. lang. reflect. method; import Java. util. collections; import Java. util. hashmap; import Java. util. map; import Org. dom4j. document; import Org. dom4j. element; import Org. dom4j. io. saxreader; public class sjxmlapplicationcontext implements applicationcontext {// Save the private map of all the bean objects in the single-profit mode in the container <string, Object> objpool = collections. synchronizedmap (n EW hashmap <string, Object> (); // Save the Document Object private document DOC in the corresponding file; // Save the root element private element root in the configuration file; public sjxmlapplicationcontext (string filepath) throws exception {saxreader reader = new saxreader (); Doc = reader. read (new file (filepath); root = Doc. getrootelement (); initpool (); initprop ();} public object getbean (string name) throws exception {object target = objpool. get (name); // For Singleton B EAN, the container has initialized all bean instances if (target. getclass ()! = String. class) {return target;} else {string clazz = (string) target; // For prototype, the attribute value return class is not injected. forname (clazz ). newinstance () ;}/// Initialize all Singleton beanprivate void initpool () throws exception {// traverse each <bean... /> element for (Object OBJ: Root. elements () {element beanele = (element) OBJ; // obtain the element's ID attribute string beanid = beanele. attributevalue ("ID"); // obtain the class attribute string beanclazz = beanele of the element. attribu Tevalue ("class"); // get the scope attribute of the element string beanscope = beanele. attributevalue ("Scope"); // if the scope attribute does not exist or is singletonif (beanscope = NULL | beanscope. equals ("Singleton") {// create a bean instance from the default constructor and put it into the objpool. put (beanid, class. forname (beanclazz ). newinstance ();} else {// for non-singleton beans, the class name objpool that stores the bean implementation class. put (beanid, beanclazz) ;}} private void initprop () throws exception {for (Object OBJ: Root. Elements () {element beanele = (element) OBJ; // obtain the element's ID attribute string beanid = beanele. attributevalue ("ID"); // get the scope attribute of the element string beanscope = beanele. attributevalue ("Scope"); If (beanscope = NULL | beanscope. equals ("Singleton") {// retrieve the bean Instance Object bean = objpool specified by the objpool. get (beanid); // traverse each <property... /> element for (Object prop: beanele. elements () {element propele = (element) prop; // obtain the value of the name attribute string P Ropname = propele. attributevalue ("name"); // obtain the value of the value attribute string propvalue = propele. attributevalue ("value"); // obtain the element's ref attribute string propref = propele. attributevalue ("Ref"); // uppercase string propnamecamelize = propname. substring (0, 1 ). touppercase () + propname. substring (1, propname. length (); // If <property... /> the value attribute value of an element exists in if (propvalue! = NULL & propvalue. length ()> 0) {method setter = bean. getclass (). getmethod ("set" + propnamecamelize, String. class); // execute setter injection setter. invoke (bean, propvalue);} If (propref! = NULL & propref. length ()> 0) {// obtain the bean instance object target = objpool to be injected by dependency. get (propref); // The specified bean instance does not exist in the objpool if (target = NULL) {// handle the case where Singleton bean depends on prototype bean} // define the setter method setter = NULL required for injection; // traverse all the interfaces implemented by the target object for (class superinterface: target. getclass (). getinterfaces () {setter = bean. getclass (). getmethod ("set" + propnamecamelize, superinterface); // if the method corresponding to this interface is obtained successfully, the loop break is directly jumped out ;} // If the setter method is still null // you can directly obtain the target implementation's setter method if (setter = NULL) {setter = bean. getclass (). getmethod ("set" + propnamecamelize, target. getclass ();} // execute setter injection. invoke (bean, target );}}}}}}
package com.fsti.factory.factorybean;public class Computer {private Output out;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Output getOut() {return out;}public void setOut(Output out) {this.out = out;}public Computer() {}public Computer(Output out) {this.out = out;}public void keyIn(String str) {out.getData(str);}public void print() {out.out();}}
Package COM. fsti. factory. factorybean; public class ioctest {public static void main (string [] ARGs) throws exception {applicationcontext CTX = new sjxmlapplicationcontext ("bean. XML "); computer c = (Computer) CTX. getbean ("computer"); C. keyin ("factory model"); C. keyin ("simple factory mode (Spring mode)"); C. print (); system. out. println (C. getname (); system. out. println (CTX. getbean ("now "));}}
<? XML version = "1.0" encoding = "UTF-8"?> <Beans> <bean id = "computer" class = "com. fsti. factory. factorybean. computer "> <property name =" name "value =" factory bean method "/> <property name =" out "ref =" Printer "/> </bean> <Bean id = "Printer" class = "com. fsti. factory. factorybean. printer "> </bean> <bean id =" betterprinter "class =" com. fsti. factory. factorybean. betterprinter "> </bean> <bean id =" now "class =" Java. util. date "Scope =" prototype "> </bean> </beans>