Example:
Properties File: Beans.properties
Articledao=cn.com.leadfar.cms.backend.dao.impl.articledaoimpl
Channeldao=cn.com.leadfar.cms.backend.dao.impl.channeldaoimpl
When you use this property, the class is configured 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 get the implementation class name of the specific DAO {props.load (Thread.CurrentThread (). Getcontextclassloader
(). getResourceAsStream ("beans.properties"));
catch (IOException e) {e.printstacktrace ();
The 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; }
}