如何在JavaEE中注入外部定义的类
假设我想使用一个如何在JavaEE中注入外部定义的类,java,spring,jakarta-ee,dependency-injection,Java,Spring,Jakarta Ee,Dependency Injection,假设我想使用一个Foo类,该类由用JavaSE编写的外部库定义(并且假设我无法编辑它) 在Spring中,我可以通过以下方式将此类声明为bean: @Bean public Foo foo() { return new Foo(); } 然后,可以执行DI: @Autowired private Foo foo; 在JavaEE中是否有同样的方法(如果可能,通过避免继承)?在CDI中,您甚至不必注释pojo 假设已在外部库中声明了以下内容: public class MyServic
Foo
类,该类由用JavaSE编写的外部库定义(并且假设我无法编辑它)
在Spring中,我可以通过以下方式将此类声明为bean:
@Bean
public Foo foo() {
return new Foo();
}
然后,可以执行DI:
@Autowired
private Foo foo;
在JavaEE中是否有同样的方法(如果可能,通过避免继承)?在CDI中,您甚至不必注释pojo 假设已在外部库中声明了以下内容:
public class MyService{}
然后在您的项目中,您可以有一个无状态ejb或另一个CDIBean
@Stateless
public class MyController{
@Produces
public MyService getMyService(){
return new MyService();
}
}
现在从控制器中的其他人开始
@SessionScoped
@Named
public class MySessionController implements Serializable{
//simple, just inject and the producer method will be called automatically
@Inject
private MyService service;
}
您需要从url检查制作人和原型:
在CDI中,您甚至不必对pojo进行注释 假设已在外部库中声明了以下内容:
public class MyService{}
然后在您的项目中,您可以有一个无状态ejb或另一个CDIBean
@Stateless
public class MyController{
@Produces
public MyService getMyService(){
return new MyService();
}
}
现在从控制器中的其他人开始
@SessionScoped
@Named
public class MySessionController implements Serializable{
//simple, just inject and the producer method will be called automatically
@Inject
private MyService service;
}
您需要从url检查制作人和原型:
您听说过CDI吗?是的,但我想知道是否有办法避免继承。你听说过CDI吗?是的,但我想知道是否有办法避免继承。