Spring参考手册说:

最好将Spring单例的范围描述为“每个容器和每个bean”。

考虑以下代码片段:

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml")
MyBean myobj=(MyBean)context.getBean("myBean"); //myBean is of singleton scope.
MyBean myobj1=(MyBean)context.getBean("myBean");


per container表示如果我们执行两次context.getBean("myBean");,它将返回相同的bean,即myobj==myobj1true

但是,上述声明中per bean中的per container and per bean是什么意思?

最佳答案

以简单的方式

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml")
MyBean myobj=(MyBean)context.getBean("myBean"); //myBean is of singleton scope.

ApplicationContext context1= new ClassPathXmlApplicationContext("Beans.xml")
MyBean myobj1=(MyBean)context1.getBean("myBean");

myobj==myobj1 would not be same

关于java - Spring 单例范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11631047/

10-10 11:36