Example:
Attribute file: Beans. Properties
Articledao = cn.com. leadfar. CMS. backend. Dao. impl. articledaoimpl
Channeldao = cn.com. leadfar. CMS. backend. Dao. impl. channeldaoimpl
When this attribute is used, the class configuration is as follows:
Propertiesbeanfactory. Java
Package cn.com. leadfar. CMS. utils; import Java. io. ioexception; import Java. util. properties; import cn.com. leadfar. CMS. backend. dao. articledao; import cn.com. leadfar. CMS. backend. dao. channeldao; public class propertiesbeanfactory implements beanfactory {properties props = new properties (); Public propertiesbeanfactory () {// read the configuration file to obtain the specific DAO implementation class name try {props. load (thread. currentthread (). getcontextclassloader (). getresourceasstream ("beans. properties ");} catch (ioexception e) {e. printstacktrace () ;}} public articledao getarticledao () {string classname = props. getproperty ("articledao"); try {class clz = Class. forname (classname); Return (articledao) clz. newinstance ();} catch (classnotfoundexception e) {e. printstacktrace ();} catch (instantiationexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace ();} return NULL;} public channeldao getchanneldao () {string classname = props. getproperty ("channeldao"); try {class clz = Class. forname (classname); Return (channeldao) clz. newinstance ();} catch (classnotfoundexception e) {e. printstacktrace ();} catch (instantiationexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace ();} return NULL ;}}
The usage is as follows:
For more information, see video lecture 33 of Li tengfei CMS, a student from Shang xuetang.