WELCOME Abdennour : Software engineer

Nov 11, 2012

Spring bean thread scope



 How beans are defined in the IoC container ?
Spring uses the notion of bean scopes to answer to this question .


we have 6 scopes : 
  >singleton, prototype, thread ,request, session, and globalSession . 

the new Scope in the Spring 3.0 is thread
Note: Singleton, prototype ,request, session, and globalSession Scopes are explained in this tutorial.

Thread Scope  Utility : 
>it Creates a bean instance per thread. Similar to request scope.

Thread Scope VS Request Scope:
>Thread Scope is available to all context implemenetation scope .
>Request Scope is available only to application contexts (web-freindly)


Note : Thread Scope requires registration with the CustomScopeConfigurer bean.


Illustration of Thread Scope Behavior :





1.Create Maven Project : 


2.Edit pom.xml file & add Spring 3 Dependencies : 


 <properties>
    <spring.version>3.0.5.RELEASE</spring.version>
  </properties>
  <dependencies>
    <!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
  </dependencies>

_______________________________

3.Create Class for Spring instanciation Test : 
>slm.abdennour.springcore.Cv

package slm.abdennour.springcore;
import java.util.Random;
/**
*
* @author Abdennour عبد النور التومي
*
* @see http://abdennour-insat.blogspot.com
* @since 10 nov. 2012
*/
public class Cv {
    private String fullName;
    public Cv() {
     this.fullName=getRandomString();
}
    public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public void display(){
     System.out.println(this.fullName);
    }
public String getRandomString(){
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 20; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
String output = sb.toString();
return output;
}
}


4.Create Runnable Class & Main Method:


package slm.abdennour.springcore;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author Abdennour عبد النور التومي
*
* @see http://abdennour-insat.blogspot.com
* @since 11 nov. 2012
*/
public class CvRun implements Runnable {
ApplicationContext ctx;
public CvRun(ApplicationContext ctx) {
// TODO Auto-generated constructor stub
this.ctx = ctx;
}
public final void run() {
// TODO Auto-generated method stub
try {
Cv cvChild = ctx.getBean("cvThread", Cv.class);
System.out.println("Cv Thread Child =>" + cvChild.getFullName());
} finally {
// TODO: handle exception
}
}
public static void main(String[] args) {
ApplicationContext appCtx=new ClassPathXmlApplicationContext("ctx.xml");
Cv cvPatent = appCtx.getBean("cvThread", Cv.class);
System.out.println("Cv Thread Parent =>" + cvPatent.getFullName());
//Cv THread
CvRun cvrun=new CvRun(appCtx);
new Thread(cvrun).start();
//Cv Simple
System.out.println("Simple");
for (int j = 0; j < 5; j++) {
System.out.print("cv Simple "+j+"=>");
((Cv)appCtx.getBean("cvSimple", Cv.class)).display();
}
}
}

5.Add two beans in Spring Config XML : 
one for singleton scope (which is the default scope)
>other for thread scope .


    <bean
        id="cvSimple"
        class="slm.abdennour.springcore.Cv"
         >
        
    </bean>
    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer" >
        <property name="scopes" >
            <map>
                <entry key="thread" >
                    <bean class="org.springframework.context.support.SimpleThreadScope" />
                </entry>
            </map>
        </property>
    </bean>
    <bean
        id="cvThread"
        class="slm.abdennour.springcore.Cv"
         scope="thread">
        
    </bean>


 
6.Run & Result :
 As the default scope is Singleton, cvSimple beans are the Same for all instanciation of Spring Factory container.  

7.Source Code : 





                              

2 comments: