[ACCEPTED]-Loading Spring context with specific classloader-classloader

Accepted answer
Score: 14

Many Spring Context Loader (for example 7 ClassPathXmlApplicationContext ) are subclass of DefaultResourceLoader.

DefaultResourceLoader has a constructor where you can 6 specify the Classloader and also has a setClassLoader method.

So 5 it is your task to find a constructor of 4 the Spring Context Loader you need, where 3 you can specify the classloader, or just 2 create it, and then use the set to set the 1 classloader you want.

Score: 6
    final ClassLoader properClassLoader = YourClass.class.getClassLoader();

    appContext = new ClassPathXmlApplicationContext("application-context.xml") {

        protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {

See here if you are doing this for OSGI 1 purposes: How do I use a Spring bean inside an OSGi bundle?

Score: 0

The org.springframework.context.support.ClassPathXmlApplicationContext class is here for you.


Score: 0

People who are using spring boot and wants 2 to use custom classloader to create application 1 context can do it like this:

public class Application

public static void main(String[] args) {
        SpringApplication app =
            new SpringApplication(Application.class);
        ResourceLoader resourceLoader = new DefaultResourceLoader();

        YourClassLoaderObject yourClassLoaderObject = new YourClassLoaderObject();

        context = app.run(args);

More Related questions