简体   繁体   English

为什么以及在哪些情况下使用不同的类加载器两次加载 bean?

[英]Why and in which cases spring load bean twice using different classLoaders?

Recently I visited an interview.最近我访问了一个采访。

I was asked:有人问我:

MyClass myClass =(MyClass) applicationContext.getBean("myClass");

throws ClassCastException抛出ClassCastException

but applicationContext.getBean("myClass").getClass() returns MyClass .applicationContext.getBean("myClass").getClass()返回MyClass

I was surprised about the question.我对这个问题感到惊讶。 I could reply only that classes loaded by different classloaders.我只能回复由不同类加载器加载的类。

  1. How to achieve this ?如何实现这一目标?
  2. Why Spring uses different classloaders ?为什么 Spring 使用不同的类加载器?

Yes different classloader will lead to this scenario.是的,不同的类加载器会导致这种情况。 This is not that common scenario however some application uses multiple classloaders (most common examples are containers and application servers).这不是常见的场景,但是某些应用程序使用多个类加载器(最常见的示例是容器和应用程序服务器)。 You can reproduce it你可以重现它

You can initialize spring application context by specifying URLClassLoader您可以通过指定URLClassLoader来初始化 spring 应用程序上下文

Have your class located at让你的班级位于

/home/jigar.joshi/foo/package/MyClass

and configure application context to use URLClassLoader like this并配置应用程序上下文以像这样使用 URLClassLoader

URL[] classURLs = { new URL("file:///home/jigar.joshi/foo") };
URLClassLoader urlClassLoader = new URLClassLoader(classURLs);
ApplicationContext context = new     ClassPathXmlApplicationContext("spring/applicationContext.xml") {
            protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
                super.initBeanDefinitionReader(reader);
                reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
                reader.setBeanClassLoader(urlClassLoader);
                setClassLoader(urlClassLoader);
            }
        };

        MyClass m = context.getBean("MyClass");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用两个不同的类加载器来加载相同类的两个孩子 - Using two different classloaders to load two children of the same class 为什么将Spring bean初始化两次 - Why gets the spring bean initialized twice 如何使用两个不同的类加载器加载同一类 - How to load same class with two different classloaders 在Spring Boot和Logback中由不同的Classloader加载的类 - Classes loaded by different Classloaders in Spring Boot and Logback 使用不同的类加载器进行不同的JUnit测试? - Using different classloaders for different JUnit tests? 使用不同的类加载器在Java中动态创建lambda - Dynamically create lambda in Java using different classloaders 可以用不同的参数化弹簧两次注入样品豆吗? - Can spring inject sample bean twice with different parameterization? 使用 spring boot 在 TestNg 测试用例中提供 BeanFactory Bean - Provide BeanFactory Bean in TestNg test cases using spring boot 当通过Spring持久化数据时,不同的类加载器会导致ClassCastException - Different classloaders cause ClassCastException when persisting data via Spring Spring Data,EclipseLink和SimpleLoadTimeWeaver; 由不同的ClassLoader加载的域类 - Spring Data, EclipseLink, and SimpleLoadTimeWeaver; domain classes loaded by different ClassLoaders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM