Java implementation reflection, dynamic configuration

Source: Internet
Author: User

java  Implement reflection//first define an interface Ishow.javapublic interface ishow () {    void show ( STRING STR);} Establish an Demo.java file implementation interface Public class demo implements ishow{    public  void show (STRING STR) {        ssytem.out.println (str);     }}//build Test.java file to test Public class test{public static void main (string args[]) {    string str = "P1.demo";   //Package name + reflected class name     try () {     class c1 =class.forname (str);     try () {         ishow a =c1.newinstance ();    //      class loading mechanism         a.show ("QQ");     }catch ( Instantiationexception e) {        E.PRINTSTACKTRAce ();     }catch (illegalaccessexception e) {         e.printstacktrace ();     }catch (classnotfoundexception e) {          e.printstacktrace ();             }        }    }}//java Implementing Dynamic Configuration   Implement reflection//ishow  and demo  invariant Public interface ishow () {    void show ( STRING STR);} Public class demo implements ishow{    public void show ( STRING STR) {        ssytem.out.println (str);     }} Build propertiesutil.javaimport java.util.properties;public class propertiesutil{     properties p = new properties ();  //new  a Properties object   &nBsp; try () {        p.load ( PropertiesUtil.class.getResourceAsStream ("show.properties")); load Show.properties    }catch ( Ioexception e) {    e.printstacktrace ();    }     return p;} Build  show.properties file show=p1.demo//build Test.java file to test public class test{public static  Void main (string args[]) {///Here the package + class name is loaded through the dynamic file Show.properties    properties p  =properties.getshowproperties ();    string str = p.getproperty ("show");     try () {    class c1 =class.forname (str);     try () {        ishow a =c1.newinstance ();     //     class loading mechanism         a.show ("QQ");     }catch (INSTANTIATIONEXCEPTion e) {        e.printstacktrace ();     } catch (illegalaccessexception e) {        e.printstacktrace ();     }catch (classnotfoundexception e) {          e.printstacktrace ();            }         }    }}



Java implementation reflection, dynamic configuration

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.