Ele*_*ist 0 tapestry inversion-of-control
我有以下课程:
public class MyClass {
@Inject
private MyAnotherClass myAnotherClass;
public MyClass() {
//Perform operations on myAnotherClass.
}
}
Run Code Online (Sandbox Code Playgroud)
我需要在构造函数中做一些需要实例的东西myAnotherClass.不幸的myAnotherClass是,在构造函数中的代码运行后注入,这意味着我正在执行操作null...
我当然可以MyAnotherClass myAnotherClass = new MyAnotherClass()直接在构造函数中将它实例化为经典的方式(),但我不认为在这种情况下做正确的事情.
你会建议什么解决方案来解决这个问题?
最佳选择:
public class MyClass {
private final MyAnotherClass myAnotherClass;
public MyClass(MyAnotherClass other) {
this.myAnotherClass = other;
// And so forth
}
}
Run Code Online (Sandbox Code Playgroud)
然后T5-IoC将使用构造函数注入,因此不需要自己"新" MyClass.有关详细信息,请参阅定义Tapestry IOC服务.
或者:
public class MyClass {
@Inject
private MyAnotherClass myAnotherClass;
@PostInjection
public void setupUsingOther() {
// Called last, after fields are injected
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
194 次 |
| 最近记录: |