In spring 2.0, the bean scope mechanism of spring can be extended. This means that you can use not only the predefined bean scopes that spring provides, but you can also define your own scopes, even redefine existing scopes (not advocated, and you can't overwrite the built-in singleton and prototype scopes).
Scopes are defined by interface Org.springframework.beans.factory.config.Scope. To integrate your own custom scope into the spring container, you need to implement that interface. It is very simple in itself, with only two methods for the underlying storage mechanism to get and delete objects. The custom scope may be beyond the scope of this reference manual, but you can refer to the scope implementation provided by spring, so as to get started on writing your own scope implementation.
After implementing one or more custom scopes and testing them, the next step is how to get the spring container to recognize your new scope. The Configurablebeanfactory interface declares the primary method for registering a new scope for the spring container. (The interface is implemented by most beanfactory implementation classes that are published with spring); The main methods of the interface are as follows:
void Registerscope (String scopeName, scope scope);
Registerscope (..) The first parameter of the method is the globally unique name associated with the scope, and an example of that name in the spring container is singleton and prototype. Registerscope (..) The second parameter of the method is an instance of the custom scope implementation that you intend to register and use.
Suppose you have written your own custom scope implementation and have registered it:
not exist; I made it up to the sake of this example
Scope customscope = new Threadscope ();
Beanfactory.registerscope ("thread", scope);
You can then create a bean definition that matches the scope rules for custom scope as follows:
scope= "Thread"/>
If you have your own custom scope implementation, you can not only register your custom scope programmatically, but also use Beanfactorypostprocessor implementations: The Customscopeconfigurer class, which registers scope in a declarative manner. The Beanfactorypostprocessor interface is one of the basic ways to extend the spring IOC container, as described in Beanfactorypostprocessor in this chapter.
The method of registering a custom scope declaratively by using Customscopeconfigurer is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" XSI:SC hemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.0.xsd "> <bean class=" Org.springframework.beans.factory.config.CustomScopeConfigurer "> & Lt;property name= "Scopes" > <map><entry key= "thread" value= "Com.foo.ThreadScope"/></map> </property> </bean> <bean id= "bar" class= "X.y.bar"scope= "Thread"> <property name= "name" value= "Rick"/> <aop:scoped-proxy/> </bean> <bean Id= "foo" class= "X.y.foo" > <property name= "Bar" ref= "bar"/> </bean> </beans>
Customscopeconfigurer allows you to specify the actual class instance as the entry value, or you can specify the actual scope implementation class instance; For details, see Javadoc for the Customscopeconfigurer class.