如何从 Injeactable 的调用者 class 解析?
How to resolve from an Injeactable its caller class?
假设我一个 class A 注入了一个 class B。在 class B 中,我需要隐含地获取注入 class 的 class ] B. 所以在这种情况下,这将是 class A.
有谁知道如何以可靠的方式接收它?
也试过了,但这给了我记录器而不是它的调用者。
Something like
@Stateless
@Dependent
public class Logger {
@Inject
InjectionPoint ip;
@Asynchronous
public void doSomething(){
ip.getMember().getDeclaringClass().getName()
}
}
看来我找到了解决办法。
做了一个帮手Class。其中包含一个@AroundInvoke 方法。
@AroundInvoke
public Object injectMap(InvocationContext ic) throws Exception {
StackTraceElement element = Thread.currentThread().getStackTrace()[CLASS_NAME_ELEMENT];
return ic.proceed();
}
在 Class A 我注释了需要的方法 使用注射剂 class B with:
@Interceptors(ContextHelper.class)
这似乎符合我的要求。
如果我们谈论的是 @Dependent
个作用域 bean,则有 a way documented in the CDI spec。
一般的想法是,CDI 允许您注入一个名为 InjectionPoint
的对象,您可以从中获取有关哪个 bean 注入了这个 bean 的信息。
这是一个简短的片段:
@Dependent //if you don't declare any scope, it's @Dependent by default
public class MyBean {
@Inject
InjectionPoint ip;
public void doStuff() {
// gives you the name of declaring class
ip.getMember().getDeclaringClass().getName();
}
}
或者,您可以在创建 bean 期间在 bean 中使用构造函数注入来处理此问题。它可能更接近您的目标:
@Dependent //if you don't declare any scope, it's @Dependent by default
public class MyAnotherBean {
public MyAnotherBean(InjectionPoint ip) {
// CDI will inject InjectionPoint automatically
ip.getMember().getDeclaringClass().getName();
}
}
再次请注意,此 仅适用于 @Dependent
!为什么?好吧,因为 @Dependent
每个注入点创建新实例并且不使用代理。因此,您还准确地知道您为谁创建此实例。其他作用域,例如 @RequestScoped
、@SessionScoped
等等,确实使用代理,因此您只是在 CDI 中实例化一个对象,然后在请求注入时传递代理。
假设我一个 class A 注入了一个 class B。在 class B 中,我需要隐含地获取注入 class 的 class ] B. 所以在这种情况下,这将是 class A.
有谁知道如何以可靠的方式接收它?
也试过了,但这给了我记录器而不是它的调用者。
Something like
@Stateless
@Dependent
public class Logger {
@Inject
InjectionPoint ip;
@Asynchronous
public void doSomething(){
ip.getMember().getDeclaringClass().getName()
}
}
看来我找到了解决办法。
做了一个帮手Class。其中包含一个@AroundInvoke 方法。
@AroundInvoke
public Object injectMap(InvocationContext ic) throws Exception {
StackTraceElement element = Thread.currentThread().getStackTrace()[CLASS_NAME_ELEMENT];
return ic.proceed();
}
在 Class A 我注释了需要的方法 使用注射剂 class B with:
@Interceptors(ContextHelper.class)
这似乎符合我的要求。
如果我们谈论的是 @Dependent
个作用域 bean,则有 a way documented in the CDI spec。
一般的想法是,CDI 允许您注入一个名为 InjectionPoint
的对象,您可以从中获取有关哪个 bean 注入了这个 bean 的信息。
这是一个简短的片段:
@Dependent //if you don't declare any scope, it's @Dependent by default
public class MyBean {
@Inject
InjectionPoint ip;
public void doStuff() {
// gives you the name of declaring class
ip.getMember().getDeclaringClass().getName();
}
}
或者,您可以在创建 bean 期间在 bean 中使用构造函数注入来处理此问题。它可能更接近您的目标:
@Dependent //if you don't declare any scope, it's @Dependent by default
public class MyAnotherBean {
public MyAnotherBean(InjectionPoint ip) {
// CDI will inject InjectionPoint automatically
ip.getMember().getDeclaringClass().getName();
}
}
再次请注意,此 仅适用于 @Dependent
!为什么?好吧,因为 @Dependent
每个注入点创建新实例并且不使用代理。因此,您还准确地知道您为谁创建此实例。其他作用域,例如 @RequestScoped
、@SessionScoped
等等,确实使用代理,因此您只是在 CDI 中实例化一个对象,然后在请求注入时传递代理。