Wu Yuxiong - a natural framework for the development of natural JAVA SPRING study notes: Spring in Bean scopes

Scope type 
Spring container during initialization Bean one example, but specifies the scope of this example. Spring3 Bean defines five for the scope, as follows. 
. 1 ) singleton 
Singleton, a singleton defined Bean Spring container is only one example, which is the default scope Bean. 
2 ) prototype 
Prototype mode, each time to obtain prototype defined by Bean Spring container, which will create a new instance of the Bean. 
. 3 ) Request 
in a HTTP request, the container will return the same instance of the Bean. And different HTTP request returns a different example, only within the scope of the current HTTP Request. 
. 4 ) the session 
in a HTTP Session, the container will return the same instance of the Bean. And different HTTP request returns a different example, only within the scope of the current HTTP Session. 
. 5 ) Global the Session 
in a global HTTP Session, the container will return the same instance of the Bean. The scope is only effective when used portlet context.
singleton scope 
singleton Spring container is the default scope when a scope is singleton Bean, Bean Spring container there will be only one shared instance, and all requests for the Bean, as long as the id matches Bean definitions, We will only return the same instance of Bean. 
Typically, such non-singleton Bean for session state (e.g. DAO layer, layer-Service), it is the ideal choice. 
Spring configuration file may be used <the bean> scope attribute of the element, as defined Bean scopes singleton, its configuration is as follows:
 <the bean ID = "Person" class = "com.mengma.scope.Person" scope = "singleton" /> 
create a src directory under the project a package called com.mengma.scope to create the Person class in this package, do not need to add any member of the class, and then create a Spring configuration file applicationContext.xml after the above-described manner Bean defined in the configuration file, the last named PersonTest a test class to create, edit, as shown below. 
Package com.mengma.scope;
 Import org.junit.Test;
 Import org.springframework.context.ApplicationContext;
org.springframework.context.support.ClassPathXmlApplicationContext;
 public  class PersonTest { 
    @Test 
    public  void Test () {
         // definition of the configuration file Spring Path 
        String XMLPath = "COM / mengma / scope / the applicationContext.xml" ;
         // initialize Spring container, loading a configuration file, and bean instantiate 
        the applicationContext applicationContext = new new the ClassPathXmlApplicationContext ( 
                XMLPath); 
        // output obtained example 
        System.out.println (to applicationContext.getBean ( "Person" )); 
        System.out.println (to applicationContext.getBean ( "Person" ));  
    }
}
Use JUnit test run () method, after the successful operation, the output test console

prototype scopes 
use the prototype scope of Bean will creates a new instance each time the Bean Bean request. Therefore Bean (eg Struts2 Action class) need to maintain session state should use the prototype scope. 
Spring in the configuration file, to be defined as the prototype scope Bean, simply <the bean> scope attribute value of the element is defined as the prototype, which is shown in the following sample code:
 <the bean ID = "Person" class = "com.mengma .scope.Person "scope =" prototype "/ > 
can be seen, the output results of the two is not the same, indicating that in the prototype scope, Spring container creates two different Person instance.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12129242.html