java - Load a class with all it's components from outter jar -
i have code:
// getting jar url contains target class url[] classloaderurls = new url[]{new url("linktojar")}; // create new urlclassloader urlclassloader urlclassloader = new urlclassloader(classloaderurls); // load target class class<?> beanclass = urlclassloader.loadclass("com.my.class"); // create new instance loaded class constructor<?> constructor = beanclass.getconstructor(); object beanobj = constructor.newinstance(); // getting method loaded class , invoke method method = beanclass.getmethod("sayhello"); method.invoke(beanobj);
it's supposed call sayhello method needs 1 bean work.
the method prints "hello %name", name @autowired string.
the call method working, problem it's saying "hello" because string not being autowired, , it's "null".
how can make autowired possible, if context in jar i'm calling , seems not being load??
thanks.
edit::
the idea have like:
applicationcontext context = new classpathxmlapplicationcontext( "classpath*:**/applicationcontext*.xml"); myadder myadder = (myadder) context.getbean("myadder");
in order load context, don't know how load context outter jar.
how expect work, spring has no idea external classes - out of application context. injecting indeed looks "magic" done spring container.
you have use own application context, , bean "the spring way".
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html
one way around inject required string current context, , pass argument method.
another, more "inject style" method, set string using reflection - more or less how done spring container internally.
Comments
Post a Comment