一个Java反射机制例子
來源:http://blog.csdn.net/loyoveui/archive/2007/06/22/1662154.aspx
package test;
import java.lang.reflect.Method;
public class InvokeTest {
/**
*
* main 方法
* @param args
* void
*/
public static void main(String[] args) {
try {
InvokeTest invokeTest = new InvokeTest();
//1.第一步获取方法的映射
//String[] realArgs = {"",""};//定义一个与execute方法第1个参数对应的String数组(注意此数组只为获取方法的映射)
//Integer in = new Integer(0);//定义一个与execute方法第2个参数对应的Integer对象
//Class[] c_args = new Class[2];
//c_args[0] = realArgs.getClass();//分别获取以上两个参数的class
//c_args[1] = in.getClass();
//Method method = invokeTest.getClass().getMethod("execute", c_args);//返回值为test方法的映射(类型Method)
/**
* 注意,以上几句(11-16行)可以写成下面一句
* 目的是获取execute方法的映射,第一个参数是方法,第二个参数是execute方法所需要的参数列表,类型是class
* 所以当execute方法没有参数时,getMethod第二个参数为null即可
*/
Method method = invokeTest.getClass().getMethod("execute",
new Class[] { String[].class, Integer.class });
//2.第二步调用方法
//String[] a1={"zhong","cheng"};//这里的数组用于做真正调用时的参数
//Integer a
相关文档:
一直被同步搞得晕头转向,今天遇到了要写静态方法,总担心会不会有同步问题,结果看了以下一席话,豁然开朗~~
Every method of java will have a stack, and every invokation on that
method will have it's own 'stack frame'. So the locale data of one
method invokation will not affect others.
Please do not c ......
本文来自CSDN博客,转载出处:http://blog.csdn.net/royaki/archive/2009/05/07/4159279.aspx
1、建立一个Servlet并且实现Filter接口
该类需要实现Filter接口中的init() doFilter() destory()方法
其中init()方法自动在项目启动的时候加载,doFilter()在调用xml配置的路径是加载,d ......
1、建立一个Servlet并且实现Filter接口
该类需要实现Filter接口中的init() doFilter() destory()方法
其中init()方法自动在项目启动的时候加载,doFilter()在调用xml配置的路径是加载,destory()方法在退出项目的时候进行。
public class TestFilter implements Filter{
......
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Vector;
/*虽然现在用APACHE COMMONS DBCP可以非常方便的建立数据库连接池, ......