Java configuration file path, reflection

Source: Internet
Author: User
Tags date1

Package cn.com. java. wwh. www;


Import java. io. IOException;
Import java. io. InputStream;
Import java. util. Date;
Import java. util. Properties;


/**
* @ Class:
*
*
* @ Author
* @ Version 1.0
* @ Creation Time: 11:48:06 AM
*
*/
Public class NewInstanceDemo2 {

Private static Properties p;

Private static String fileName = "info. properties ";

Static {
P = new Properties ();
// Create an input stream (create a stream object from the resource file to the Program)
InputStream inStream = Thread. currentThread (). getContextClassLoader ()
. GetResourceAsStream (fileName );


Try {
// Load the resource stream
P. load (inStream );
} Catch (IOException e ){
E. printStackTrace ();
}
}

Private static String getValue (String key)
{
Return p. getProperty (key );
}

Private static Object getBean (String name) throws Exception {
Class Claz = Class. forName (name );
Return claz. getConstructor (String. class). newInstance (" ");
}
Public static T getBean (Class Claz) throws Exception {
Return claz. newInstance ();
}

Public static T getBean (String className, Class Claz) throws Exception
{
Class Linoleic = (Class ) Class. forName (className );
Return linoleic. newInstance ();
}
Public static void main (String [] args) throws Exception {
// Obtain the full qualified name from the resource file
/* String className = getValue ("string ");

Object obj = getBean (className );
System. out. println (obj );
System. out. println (className );*/
String className = getValue ("date ");
Class Claz = Class. forName (className );
Date date = (Date) claz. newInstance ();
System. out. println (date );
// Use generic processing
System. out. println ("generic processing 1 :");
Date date1 = getBean (className, Date. class );
System. out. println (date1 );
// Not connected to the configuration file
System. out. println ("generic processing 2 :");

System. out. println (getBean (NewInstanceDemo2.class ));

}

}

// Note: This configuration file obj. properties must be placed in the binfile in the Created directory folder. Otherwise, An Exception error will be reported. I have debugged this error for a long time. I checked a lot of information online and tried many methods to solve it.

Most people say that the java environment variable settings are incorrect. I re-checked the environment variable and did not make any errors. And then write the file path into the path where the file is located. That's why I put it in the bin folder!


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.