jeudi 8 janvier 2015

Spring Can not load Configuration Class when we Call it by Using Java Reflection

I have blocking when bootsrap the Spring by using Java Reflection, so I implement Spring by using Annotation Configuration then I have the following class as Spring Configuration



@Configuration
@ComponentScan(basePackages = "com.jet.reader")
public class QueueReaderConfig {

@Bean(name = "jetListener")
public JetReaderListener listener() {
return new JetReaderListener();
}

@Bean(name = "rbtReaderService")
public QueueReaderService queueReaderService() {
return new QueueReaderServiceImpl();
}
}


And then bootsrap the Spring by using the following command



context = new AnnotationConfigApplicationContext(QueueReaderConfig.class);
rbtReaderService = context.getBean(QueueReaderServiceImpl.class);


It's run normally when I running my application by using JUnit or through Main class, but it display error when I running the application by using Java Reflection (The class that wrap that Spring is load by using URLClassLoader)



URL[] urls = { new URL("jar:file:" + plugin + "!/") };
URLClassLoader cl = URLClassLoader.newInstance(urls);
...
Class<?> c = Class.forName (className, true, cl);
...
Object instance = c.newInstance();
...
Method loadMethod = c.getDeclaredMethod("load", OED.class);
loadMethod.setAccessible(true);
loadMethod.invoke(instance, param);


The script produce the following exception



2015-01-08T21:17:43.000+1100 com.jet.reader.executor.JetQueueReaderExecutor 57 (DEBUG):java.lang.IllegalStateException: Cannot load configuration class: com.jet.reader.config.QueueReaderConfig
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.jet.godi.GodiAppConsoleMain.execute(GodiAppConsoleMain.java:143)
at com.jet.godi.GodiAppConsoleMain.executePlugins(GodiAppConsoleMain.java:92)
at com.jet.godi.GodiAppConsoleMain.<init>(GodiAppConsoleMain.java:42)
at com.jet.godi.GodiAppConsoleMain.main(GodiAppConsoleMain.java:231)


Is anyone has the same experience with me for this case, please share your solution here


Thanks






Aucun commentaire:

Enregistrer un commentaire