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

  • SEARCH
  • Home
  • 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 8630745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:07:07+00:00 2026-06-12T09:07:07+00:00

I am having some troubles with dependency injection stuff, as I am really new

  • 0

I am having some troubles with dependency injection stuff, as I am really new to Seam I might be doing something in a wrong way!

I need to inject dependencies on a new thread which is fired from within a controller – I get no exceptions but they simply come null. First I tried simply reusing d1 (see below) within the thread but It was null, then I had this idea, to annotate this object again with @In… Unfortunately the same happened (got null)!!!

@Scope(ScopeType.CONVERSATION)
@Name("myController")
public class MyController{
    @In(create = true)
    private Dependency1 d1; //ok, gets injected with no problems!

    public void importantMethod(){
        //this part of the method is important and is fast
        //but below comes an expensive part that takes some minutes

        new Thread(new Runnable(){
            @In(create = true)
            private Dependency1 anotherD1;  //I still need d1 here!!!       

            @Override
            public void run(){
                //I want to run expensive code here.
                //A new thread is required in order to leave
                //the visitor free to go else where on the web site

                //First trial was to make d1 final and simply use it here!
                //d1.doExpensiveStuff();
            };
        }).start();
    }
}

Does anyone have any idea on why this is happening? Are there any good practices when working with DI/Seam/Threading?

  • 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-12T09:07:07+00:00Added an answer on June 12, 2026 at 9:07 am

    Injection happens only:

    1. In Seam components (in your example, MyController is a component, the anonymous Runnable you create inside is not a component).
    2. Inside a Seam lifecycle. A lifecycle is either started by a JSF request, an asynchronous execution or manually by yourself.

    So, you cannot use @In inside your thread because it is not a component and Seam will not intercept calls to it. To get hold of components within the asynchronous thread, you need to use the Seam API to start the lifecycle and get the components you need:

    @Scope(ScopeType.CONVERSATION)
    @Name("myController")
    public class MyController {
    
        @In(create = true)
        private transient Dependency1 d1;
    
        public void importantMethod() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    LifeCycle.beginCall(); // Start the Seam lifecycle
                    Dependency1 d1 = (Dependency1) Component.getInstance("dependency1");
                    d1.doExpensiveStuff();
                    LifeCycle.endCall();   // Dispose the lifecycle
                }
            }).start();
        }
    }
    

    Seam provides the @Asynchronous annotation that does just what you want here. If this annotation is used in a method of a Seam component, the method will be executed in a background thread (taken from a Seam-owned threadpool). Note that the asynchronous method will be able to use injected dependencies as if it was a normal Seam call:

    @Name("myBackgroundWork")
    public class MyBackgroundWork {
        @In private transient Dependency1 d1;
    
        @Asynchronous
        public void runInBackground() {
             d1.doExpensiveStuff();
        }
    }
    

    Then in MyController you can call the asynchronous method which will start the background work and return immediately:

    @Scope(ScopeType.CONVERSATION)
    @Name("myController")
    public class MyController {
        @In(create = true)
        private MyBackgroundWork myBackgroundWork;
    
        public void importantMethod() {
            // Execution will return immediately and thread will start
            myBackgroundWork.runInBackground();
        }
    }
    

    More info here:

    http://docs.jboss.org/seam/2.2.2.Final/reference/en-US/html/jms.html#d0e21609

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

Sidebar

Related Questions

I'm starting out with Dependency Injection and am having some trouble injecting a dependency
I'm having some troubles adding an UIView to cocos2d v2.0 since the openGlView is
guys am having some troubles with running out of memory when displaying an animation
I'm having some troubles using Session Variables as they are being used as Reference
I am using VS2012 and having some troubles publishing an mvc4 website. None of
I'm developing a website in WordPress and I'm having some troubles with the the
I want to find a struct in a vector, but I'm having some troubles.
I'm just getting started with LINQ, and I'm having some troubles. Say I wanted
I know it's possible with jQuery, but I'm having some serious troubles with this
I have some troubles with a method having a typed List parameter, inherited from

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.