我使用 Java 6 阅读了一些关于 DI 的文档,但我不确定是否完全理解。我有以下类,我想在其中注入服务:
@ManagedBean
@RequestScoped
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private MyService myService;
private List<SomeObject> someObjects;
// Getter and setter
public List<SomeObject> getSomeObjects() {
if (someObjects == null) {
someObjects = myService.find();
}
return someObjects;
}
}
服务:
public class MyServiceImpl implements MyService {
public MyServiceImpl() {
}
}
运行此代码时,不会注入 myService。请问,我做错了什么?
PS:我使用的是Tomcat 7