Spring @Scope

in Spring frameworks, sometimes you need to take control of beans scope, in that case you can add AOP jar files which will come if you use BOM, and Context set of dependencies and then easily use the @Scope annotation on beans. Below is a set of bean scope available in Spring:

  • Singleton = only one object in entire Spring container
  • prototype = a new object per every time calling applicationContext.getBean(…)
  • web based bean scope:
    • request // a new object per request – a little bit longer than prototype since that was being called by getbean method every time.
    • session // an object per session and will be renewed whenever we hit the timeout for that session
    • globalsession // an object for entire application

example:


@service("myservice")

@scope("singleton") // this is the default bean scope
//or using this @Scope(ConfirgurableFactoryBean.SCOPE_SINGLETON)

public class MyServiceImple extends MyService{

//....

}

 

 

Advertisements