By default, all the beans in spring bean are single, that is, the single-profit mode. <Bean id = "mybean" class = "... "Scope = "Singleton"/>. Singleton indicates that only one bean object called mybean will be created in the spring container. All requests to this bean are processed by this object. This bean object is shared, that is, this bean is non-thread-safe. In this case, isn't the beans injected by containers in the service and Dao layers of our three-tier framework non-thread-safe? Isn't that a problem? The answer is that the beans do not use global variables, that is, there is no shared data, so we don't have to worry about it. If shared data is available, we can set it for thread security.Singleton = "false"To ensure its security.
When will we often use non-single-profit beans? When the action is handed over to spring for management, the bean of the action generated by the container is not single-profit. Configure the following in struts. XML (struts2): <constant name = "struts. objectfactory" value = "Spring"/> (struts1 does not know how to configure it ). The bean for Action Management by the container also needs a configuration, that isScope = "prototype ".In this way, every time the page requests an action bean, a new object will be generated to process the user's request. This ensures the thread security.
For the above questions, you can also refer to the following three explanations:
Http://www.juziku.com/zhouchao/wiki/4036.htm,
Http://blog.csdn.net/mastermind/article/details/1932787 2
3. section 2.5 of chapter 2 of spring in Action (Chinese version 2): Controls bean initialization (2.5.1 bean containerization)