4

Bean のインスタンス化中に特定の URL からクラスをロードするように Spring に指示する方法はありますか? クラスパスにない場所からクラスをロードする必要があります。純粋な Java を使用している場合は使用できますURLClassLoaderが、Spring でこれを実現するにはどうすればよいですか? Spring 3.0を使用しています

4

2 に答える 2

5

拡張するすべての Spring クラスDefaultResourceLoaderは、明示的なClassLoader参照セットを持つことができます ( DefaultResourceLoader.setClassLoader(ClassLoader).

AbstractApplicationContextたまたまそれらのクラスの 1 つです。したがって、それを拡張するすべての ApplicationContext 実装 (ClassPathXmlApplicationContextおよび など FileSystemXmlApplicationContext) は、注入されたClassLoader参照を使用できます。

于 2012-01-19T22:36:11.117 に答える
0
public class Main {

    public static void main(String[] args) throws Exception {  
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AutodeployConfiguration.class);

        URL[] files = {
            new File("C:\\module1.jar").toURL(),
            new File("C:\\propertiesdirectory\\").toURL()
        };

        URLClassLoader plugin = new URLClassLoader(files, Main.class.getClassLoader());
        Thread.currentThread().setContextClassLoader(plugin);

        Class startclass = plugin.loadClass("de.module1.YourModule");
        ExternalModule start = (ExternalModule) startclass.newInstance();
        AnnotationConfigApplicationContext ivr = start.onDeploy(plugin, ctx);
    }
}


public class YourModule implements ExternalModule {

    @Override
    public AnnotationConfigApplicationContext onDeploy(ClassLoader classloader, GenericApplicationContext parent) {      
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.setClassLoader(classloader);
        applicationContext.setParent(parent);
        applicationContext.register(ModuleConcreteConfiguration.class);
        applicationContext.refresh();


        // other code

        return applicationContext;
    }
}
于 2015-03-26T19:43:48.490 に答える