我在依赖注入方面遇到了一些麻烦,因为我对 Seam 真的很陌生,我可能会以错误的方式做某事!
我需要在从控制器内触发的新线程上注入依赖项 - 我没有例外,但它们只是来了null
。d1
首先,我尝试在线程中简单地重用(见下文),但它是null
,然后我有了这个想法,再次注释这个对象@In
......不幸的是,同样的事情发生了(得到空)!!!
@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();
}
}
有谁知道为什么会这样?使用 DI/Seam/Threading 时有什么好的做法吗?