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!