更新时间:2023-12-03 13:50:22
我只能将CDI bean注入托管类中。 Struts创建了自己的实例,因此Struts Bean没有容器管理。
I only can inject your CDI beans in managed classes. Struts creates its own instances, so Struts beans are no container managed.
我不知道Struts是否具有能够注入CDI bean的插件,但是您可以查找
I don't know if Struts has a plugin able to inject CDI beans, but you can lookup the Bean Manager in JNDI tree.
public BeanManager getBeanManager() {
try {
Context jndiContext = new InitialContext();
return (BeanManager) jndiContext.lookup("java:comp/BeanManager");
} catch (NamingException e) {
// handle the exception
}
}
在您的应用程序中,可以使用以下命令:
In your application you can use this:
BeanManager manager = BeanManagerUtil.getBeanManager();
manager.getBeans(MyClass.class);
对于getBeans方法返回的每个bean,您都需要获取bean引用
For each bean returned by getBeans method you need to get the bean reference
CreationalContext ctx = manager.createCreationalContext(bean);
MyClass myClass = manager.getReference(bean, MyClass.class, ctx);
问候