java oop Chapter 11 _ reflection, BaseDao further transformation

 

Introduction: From the beginning Java5, Java cited a new concept of reflection, when the program runs, so the information can be dynamically sense the program has, the process of obtaining information is to use reflection to complete.

A,       Class categories:

  Class class is used to hold all messages for all the Java program interfaces, classes, arrays written, once we create with interfaces, classes, will be registered in the Class class an array, that is, save all the information.

  When we need to get an interface, class, you can gain access to information when they register an array of objects in Class by the object and then call some method to obtain reflection mechanism provided by the operator.

 

 

Second,       get the object of an interface, an array class or registered in the Class:

  1. 1.     obtained by calling getClass () method:

Grade grade = new Grade();

Class gradeClss = grade.getClass();

  1. 2.     get through the interface, access class class static properties:

Class studentClass = Student.class;

  1. 3.     get through the full class name:

Class driverClass = Class.forName("com.mysql.jdbc.Driver");

 

Third, the       method calls provided by the reflection mechanism Class object to complete the operation:

 

/**
     * Reflective properties acquired by Grade
     * If the method is called with the beginning of getDeclared expressed regardless of whether the resource is private, all of them visit
     */
    @Test
    public  void testGetFieds () {
        Class clazz = Grade.class;
        //Field[] fiels = clazz.getFields();
        Field[] fiels = clazz.getDeclaredFields();
        for (Field field : fiels) {
            System.out.println(field.getName());
        }
    }
    
    /**
     * Reflective properties acquired by Grade                            
     * If the method is called with the beginning of getDeclared expressed regardless of whether the resource is private, all of them visit
     */
    @Test
    public  void testGetMedthods () {
        Class clazz = Grade.class;
        //Field[] fiels = clazz.getFields();
        Method[] medthods = clazz.getDeclaredMethods();
        clazz.getSuperclass();
        for (Method medthod : medthods) {
            System.out.println(medthod.getName());
        }
    }

 

Four,       the Java default of several class loader:

Java virtual machine (jvm) will start automatically when you start several class loader, they are "system class loader", "extension class loader", "root class loader", we can also get to the first two loader, and "root class loader" inadmissibility of the acquisition.

  1)    "root class loader": loading the necessary classes when the JVM starts, as java.lang package classes.

  2)    the system class loader ": loading classes provided in the sun's JVM starts, such as: Java.util.Date.

  3)    "extension class loader": load some classes provided by third parties in the JVM starts, such as: com.mysql.jdbc.Driver.

/**
     * Exercise class loader use
     * Get a few classes in the Java class loader in the course of practice
     * System, the expansion loader can be obtained, the root class loader inadmissibility of the acquisition.
     */
    @Test
    public  void testClassLoader () {
         // Get the system class loader: 
        ClassLoader classLoader = ClassLoader.getSystemClassLoader ();
        System.out.println ( "class loader system:" + classLoader);
        
        // get extended class loader 
        ClassLoader extendClassLoader = ClassLoader.getSystemClassLoader () getParent ().;
        System.out.println ( "extension class loader:" + extendClassLoader);
        
        // trying to get the root class loader, but in reality can not get to the 
        ClassLoader BootClassLoader = ClassLoader.getSystemClassLoader () getParent () getParent ()..;
        System.out.println ( "class loader attempts to acquire the root:" + BootClassLoader);
        
    }

 

Fifth,       by the class loader loads the resources placed in the src:

  Placed in the src of resources can not directly be obtained by "/ XXX" as placed under webroot resources, and in order to get through the class loader.

  Case: BaseDao for the transformation:

  You need to write dead four parameters in connection to the database BaseDao: Drive (driverClass), url (jdbcurl), user name (user), password (password) to move a file attribute (db.properties) by the BaseDao in the class loader loads db.properties come, but also a tool class properties to read the contents of db.properties.

 

db.properties: New Source Folder in a project folder called config, create a new folder named db.properties the file, if they wanted to modify the database information, it can be modified directly in the file.

driverClass = com.mysql.jdbc.Driver
jdbcurl = jdbc:mysql://127.0.0.1:3306/myschool39
user = root
password = root

#driverClass = oracle.jdbc.driver.OracleDriver
#jdbcurl = jdbc:oracle:thin:@127.0.0.1:1521:orcl
#user = user39
#password = root

#driverClass = com.microsoft.sqlserver.jdbc.SQLServerDriver
#jdbcurl = jdbc:sqlserver://127.0.0.1:1433;databaseName = sqlserver
#user =
#password = root

BaseDao.java

package com.oop.util;

import java.io.InputStream;
/*
 * BaseDao class defines three attributes (Connection, PreparedStatement, ResultSet type) and
 * Two methods: getConnection method for acquiring connection (), close the resource method closeAll.
 */
import java.sql.*;
import java.util.Properties;

public  class BaseDao {
     // three attributes 
    protected the InputStream inputStream = null ;
    
    protected Connection ct = null;
    protected PreparedStatement pst = null;
    protected ResultSet rs = null;

    // Get the connection 
    protected Connection the getConnection () {
         the try {
            
            // 0, four parameter values acquired from the database connection properties file db.properties
             //   COM / OOP / util / db.properties use if the property file in the package util 
            
            inputStream =   the this .getClass (). GetClassLoader ( ) .getResourceAsStream ( "db.properties" );
            Properties properites = new Properties();
            properites.load(inputStream);
String driverClass
= properites.getProperty("driverClass"); String jdbcurl = properites.getProperty("jdbcurl"); String user = properites.getProperty("uset"); String password = properites.getProperty("password"); // 1 --- load (registered) drive: Specifies the link is what kind of database management system / * * Call forName (String className) method directly with Class class * Get the object registered in the Class Driver class may be utilized "reflection" of the object from all the information acquired in the Driver class. */ Class.forName(driverClass); // 2 --- acquisition and database links: you need to specify the URL of (jdbc: MySQL: // 127.0.0.1:3306/ database name), user, password. // Return value needed to achieve the object class of a connection interface to the receiving ct ct = the DriverManager.getConnection (jdbcURL, User, password); } catch (Exception e) { e.printStackTrace (); } return ct; } // Close the resource protected void closeAll () { the try { IF (RS! = Null ) { rs.close(); } if (pst != null) { pst.close(); } if (ct != null) { ct.close(); } if (inputStream != null) { inputStream.close(); } } catch (Exception e) { e.printStackTrace (); } } }

 

 

Guess you like

Origin www.cnblogs.com/zhangzimuzjq/p/11769351.html