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 ;}}