我只是想知道 Spring.xml
文件何时使用 ClassPathXmlApplicationContext
多次加载到应用程序中。例如,如果我想通过在每个类中创建 ApplicationContext
的新实例,将 Spring.xml
中定义的服务放入我的类中。这种方法是否每次都会创建新的 bean,而不破坏以前创建的 bean?这会产生任何内存问题吗?当我看到ClassPathXmlApplicationContext API时在 spring 网站中我发现了这个。此刷新是否通过销毁现有 bean 来创建现有 bean 的新 bean 定义?
ClassPathXmlApplicationContext(String... configLocations)
***Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML files and automatically refreshing the context.***
最佳答案
当同一个 xml 文件被加载多次时,spring 会多次创建相同的 bean。在大多数情况下,除了启动时间之外,这不会引起问题。但有时你会发生冲突。例如,如果您有一个正在监听 TCP 端口的 Bean,然后打开另一个尝试连接到同一端口的 Bean,则会失败。
关于java - 当 spring bean 定义文件多次加载到应用程序中时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13017066/