我需要通过Java反射api实例化几个类并在实例化类中调用'a'方法(方法调用不是通过反射)。我知道反射确实会影响应用程序的性能,但我不太确定它对网络环境的影响有多大!与使用反射实例化 Action 类的 Struts 框架一样,我的框架也将类名作为配置在文件中的字符串获取,我根据不同的请求读取并实例化它。每个类只有一个实例并为每个请求调用其方法的想法怎么样?例子,
package com.app.events;
public class event1 implements iEvent {
public event1() {
}
public void doprocess(Object info) {
// do necessary events
}
}
package com.app.events;
public class event2 implements iEvent {
public event1() {
}
public void doprocess(Object info) {
// do necessary events
}
}
config.xml
<events>
<event>com.app.events.Event2</event>
<event>com.app.events.Event1</event>
</events>
// servlet init
String clazName = parseXMLFile(); // not every time but only once, I have the bean
Class claz = Class.forName(clazName);
// how about this?
// I save this instance for later requests
events.put(request.getParameter("event"), claz.newInstance());
// later requests,I retrieve the event from the map and invoke its method,
// just a rough code...
events.get(request.getParameter("event")).doprocess(info);