복붙노트

[SPRING] 특정 클래스 로더와 함께 Spring 컨텍스트로드하기

SPRING

특정 클래스 로더와 함께 Spring 컨텍스트로드하기

내 자신의 ClassLoader 인스턴스로 Spring 컨텍스트를로드하는 방법은 무엇입니까?

해결법

  1. ==============================

    1.많은 스프링 컨텍스트 로더 (ClassPathXmlApplicationContext) )는 DefaultResourceLoader의 하위 클래스입니다.

    많은 스프링 컨텍스트 로더 (ClassPathXmlApplicationContext) )는 DefaultResourceLoader의 하위 클래스입니다.

    DefaultResourceLoader에는 Classloader를 지정할 수있는 생성자가 있으며 setClassLoader 메서드도 있습니다.

    따라서 필요한 스프링 컨텍스트 로더의 생성자를 찾고, 클래스 로더를 지정하거나, 그냥 생성 한 다음, 세트를 사용하여 원하는 클래스 로더를 설정할 수 있습니다.

  2. ==============================

    2.

        final ClassLoader properClassLoader = YourClass.class.getClassLoader();
    
        appContext = new ClassPathXmlApplicationContext("application-context.xml") {
    
            protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
                super.initBeanDefinitionReader(reader);
                reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
                reader.setBeanClassLoader(properClassLoader);
                setClassLoader(properClassLoader);
    

    OSGI 목적을 위해이 작업을 수행하는 경우 여기를 참고하십시오. OSGi 번들 내에서 Spring bean을 사용하려면 어떻게해야합니까?

  3. ==============================

    3.org.springframework.context.support.ClassPathXmlApplicationContext 클래스는 여기에 있습니다.

    org.springframework.context.support.ClassPathXmlApplicationContext 클래스는 여기에 있습니다.

  4. from https://stackoverflow.com/questions/5660115/loading-spring-context-with-specific-classloader by cc-by-sa and MIT license