Java Read configuration file properties method

Source: Internet
Author: User

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

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.