Bean scopes

Spring defines a variety of scopes can be created based on these bean scopes

Singleton Singleton entire application, create only a

When each injection Prototype Prototype or acquired by the Spring context, will create a new instance of the bean

Session session in a web application, create a bean instance for each session

Request requests in a web application, to create an instance for each request bean

Default singleton scope. Can be changed by @Scope notes, it can be used with @Bean and @Component.

@Component

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

public   class  Notepad {   .....   }

Similarly, you can use XML to configure the bean, then:

<bean   id =  "notepad"   class = "com.myapp.Notepad"   scope = "prototype" />

In this way, each injection or get bean from the Spring context will create new instances

 

Session Scope and the request scope

As e-commerce system, a bean represents a shopping cart, then if it is a singleton, then all users are added to a shopping cart, if it is the prototype (multiple cases) scope, then add cart in one place, another local can not be used. In terms of shopping cart, session scope is most appropriate. Because it is given for maximum relevance.

@Component

@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.INTERFACES)

public  ShoppingCart  cart()  {   ...........    } 

Here, the value is set to SCOPE_SESSION constant WebApplicationContext in, it will tell Spring to create an instance of the bean for each session

Another attribute @Scope proxyMode, which is arranged ScopedProxyMode.INTERFACES, to solve this property or session request scope bean into a single problem encountered bean embodiment. Such as:

@Component

public   class    StoreService{

   @Autowired

     public   void   setShoppingCart(ShoppingCart   shoppingCart){

           this.shoppingCart  = shoppingCart;

   }

....

}

Because the bean StroeService is a single embodiment, Spring context is created when it is loaded, when it is created, will attempt to ShoppingCart bean Spring poured into setShoppingCart (method), but ShoppingCart bean is a session scope, and at this time It does not exist until a user enters the system after the session is created ShoppingCar instances appear. Spring does not take actual ShoppingCart bean injected into the StoreServeice, Spring will inject a proxy to Shopping cart Bean, and ShoppingCart the same way as the proxy will be exposed, so the Service would think he was a shopping cart. But when you call the method ShoppingCart of StoreService, the agent will call them lazy and resolve real role entrusted to the session within ShoppingCart bean.

proxyMode property is set to ScopedProxyMode.INTERFACES, indicating that this agent to achieve ShoppingCart interface and call the delegate to achieve bean. If ShoppingCart is an interface, so it is possible to set. But if ShoppingCart is a specific kind of thing, Spring there is no way to create a proxy-based interface. At this point, it must use CGLib generated based on the proxy class, so if bean type is a concrete class, we must proxyMode property to: ScopedProxyMode.TARGET_CLASS, in order to show that you want to generate the target class scalable way to create a proxy. Request scope bean assembly will face the same problem, so the request scope bean should also scoped proxy way injection.

      

Scope proxy statement in XML

<bean  id="cart"  class = "com.myapp.ShoppingCart"   >

  <aop:scopde-proxy / >

</bean>

  <Aop: scopde-proxy /> is proxyMode property @Scope annotation feature the same Spring XML configuration elements, it will tell create a scope Spring bean proxy, by default, it will use CGLib create a proxy target class, However, we can proxy-target-class property set to false, and thus it is required to generate the proxy Interface.

<bean  id="cart"  class = "com.myapp.ShoppingCart"  scope="session" >

  <aop:scopde-proxy   proxy-target-class="false"/ >

</bean>

 

 

Guess you like

Origin blog.csdn.net/m0_37668842/article/details/82754979