Java method for reading the properties of the configuration file

Source: Internet
Author: User

 

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

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.