Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7865295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:02:41+00:00 2026-06-03T00:02:41+00:00

I faced this issue, while trying to autowire a runnable class and creating different

  • 0

I faced this issue, while trying to autowire a runnable class and creating different instances of it in different call and keeping it in an array.

xml configuration is :

<bean name="threadName" Class="ABC" scope="prototype" />

In my code, I am trying something like this:

public class ThreadHandler{


@Autowired
private ABC threadName;

//getter
ABC getThreadName(){
     return threadName;
}

public void someFunction(){
     List<ABC> abc = new ArrayList(ABC>();
     for (int i=0;i<SOME_CONST;i++){
          ABC tName = getThreadName();
          abc.add(tName);
          tName.start();
      }
}   

}

Let ABC be a class which is Thread/Runnable/Callable.

In this way, it throws java.lang.IllegalThreadStateException.
But, it works fine, if I use ABC tName =appContext.getBean("threadName",ABC.class);

Why does it happen?

Don’t we get a new instance while trying to get an object from getMethod?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:02:42+00:00Added an answer on June 3, 2026 at 12:02 am

    There is much better practice when you need to create Runnable/Callable and inject it into applicationContext it’s called look up method:

    Let’s consider that all Runnable/Callable classes are @Prototype and @Lazy

    @Component(value="task")
    @Scope(value="prototype")
    @Lazy(value=true)
    public class Task implements Runnable {
    
    public void run(){
    .....
    }
    
    }
    

    Now you need to Create Look up method factory:

        <bean id="taskFactory" class="x.y.z.TaskFactory">
    <lookup-method name="createTask" bean="task"/>
    </bean>
    

    Now let’s implement TaskFactory itself which is abstract class and have one abstract method :

    @Component(value="taskFactory")
    public abstract class TaskFactory {
    
        public abstract Task createTask();
    
    }
    

    Here comes the magic:

    public class ThreadHandler{
    
    @Autowired
    private TaskFactory taskFactory;
    
    
    public void someFunction(){
              Runnable task = taskFactory.createTask();
              taskExecutor.execute(task);
          }
    }   
    

    Every time you are calling createTask() method of taskFactory singleton object. you will receive completely new instance of your prototype object.

    P.S: don’t forget to add

    <context:annotation-config />
        <context:component-scan base-package="x.y.z"></context:component-scan>
    

    to enable Annotations correctly.

    hope it Helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I deployed my application on the server I faced this issue: Request.Url.ToString(); returns
I am faced with the following issue and at this point I feel like
This is an interview question I faced recently. Given an array of 1 and
hi i have faced an issue while accessing an object, in my program there
While designing my site I am constantly faced with the issue of whether its
I'm wondering if anyone has faced this issue. Let's assume I have two tables:
I have faced the same problem many times. The Same Problem was With This
I am trying to add all the elements in array using push . then
I've I'm faced to a big issue : I can't add datas to an
We faced an issue with file Merge at TFS2010. Until now, we merged files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.