这是我正在处理的代码,它在运行时使用为构造函数提供的参数实例化和创建对象。
private static void createInstancesFromSpecfication() {
String[] specifierFileNames = resourceMap.get("instances");
if (null == specifierFileNames)
return;
List<SpecifierObjects> instances = new ArrayList<SpecifierObjects>();
for (int i = 0; i < specifierFileNames.length; i++) {
try {
instances.addAll(XMLFileUtility.readXML(specifierFileNames[i],
new ClassInstantiationHandler()));
} catch (ResourceNotAvailableException | DocumentException
| ResourceDataFailureException e) {
e.printStackTrace();
}
}
for (Iterator<SpecifierObjects> iterator = instances.iterator(); iterator
.hasNext();) {
InstanceSpecifierObject instanceSpecifierObject = (InstanceSpecifierObject) iterator
.next();
try {
Class<?> cast = Class.forName(instanceSpecifierObject
.getInterfaceName());
Class<?> clazz = Class.forName(instanceSpecifierObject
.getClassName());
Constructor<?> constructor = clazz
.getConstructor(String[].class);
String[] param = instanceSpecifierObject.getFilesName();
for (int i = 0; i < param.length; i++)
System.out.println(param[i]);
Type[] params = constructor.getGenericParameterTypes();
for (int i = 0; i < params.length; i++)
System.out.println(params[i]);
Object obj = constructor.newInstance(new Object[] { param });
objects.put(instanceSpecifierObject.getObjectName(),
cast.cast(obj));
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e) {
e.printStackTrace();
}
}
}
我遇到以下异常:
instances=/home/kranti/workspace/CoreLibs/src/instanceSpecification.xml
/home/kranti/workspace/CoreLibs/src/instanceSpecification.xml
content.xml
notext.xml
class [Ljava.lang.String;
Sep 26, 2014 4:15:32 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@cb6b0a: startup date [Fri Sep 26 16:15:32 IST 2014]; root of context hierarchy
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.framew.confinit.ApplicationResource.createInstancesFromSpecfication(ApplicationResource.java:102)
at com.framew.confinit.ApplicationResource.importResource(ApplicationResource.java:59)
at com.framew.confinit.ApplicationResource.main(ApplicationResource.java:41)
Caused by: java.lang.ExceptionInInitializerError
at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:194)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:458)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:388)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
... 7 more
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:103)
... 13 more
输入,即传递给构造函数的参数值是{content.xml,notext.xml}。我不确定为什么会发生这种异常。
感谢任何帮助。提前谢谢。