Java 如何从Injactable解析其调用方类?
假设我有一个类a,它注入了aclass B。在类B中,我需要隐式地获得注入了类B的类。所以在这种情况下,这就是类a 有人知道如何以一种稳健的方式接收此信息吗 我也试过了,但这给我的不是它的调用者Java 如何从Injactable解析其调用方类?,java,jakarta-ee,dependency-injection,cdi,Java,Jakarta Ee,Dependency Injection,Cdi,假设我有一个类a,它注入了aclass B。在类B中,我需要隐式地获得注入了类B的类。所以在这种情况下,这就是类a 有人知道如何以一种稳健的方式接收此信息吗 我也试过了,但这给我的不是它的调用者 Something like @Stateless @Dependent public class Logger { @Inject InjectionPoint ip; @Asynchronous public void doSomething(){ ip.getMember().getD
Something like
@Stateless
@Dependent
public class Logger {
@Inject
InjectionPoint ip;
@Asynchronous
public void doSomething(){
ip.getMember().getDeclaringClass().getName()
}
}
看来我找到了解决办法 制作了一个助手类。其中包含@AroundInvoke方法
@AroundInvoke
public Object injectMap(InvocationContext ic) throws Exception {
StackTraceElement element = Thread.currentThread().getStackTrace()[CLASS_NAME_ELEMENT];
return ic.proceed();
}
在类A中,我将需要使用可注入类B的方法注释为:
@Interceptors(ContextHelper.class)
这似乎符合我的要求。似乎我找到了解决方案 制作了一个助手类。其中包含@AroundInvoke方法
@AroundInvoke
public Object injectMap(InvocationContext ic) throws Exception {
StackTraceElement element = Thread.currentThread().getStackTrace()[CLASS_NAME_ELEMENT];
return ic.proceed();
}
在类A中,我将需要使用可注入类B的方法注释为:
@Interceptors(ContextHelper.class)
这似乎符合我的要求。如果我们谈论的是
@Dependent
作用域bean,那么就有了
一般的想法是,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中实例化一个对象,然后在请求注入时传递代理 如果我们谈论的是@Dependent
作用域bean,那么就有了
一般的想法是,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中实例化一个对象,然后在请求注入时传递代理 虽然这可能有效,但它似乎相当核心。无论如何,我建议使用CDI拦截器(例如注释@Interceptor
和@InterceptorBinding
),而不是像这里这样使用EJB拦截器。虽然这可能会起作用,但EJB拦截器并不是CDI中的一流公民。无论如何,我建议使用CDI拦截器(例如注释@Interceptor
和@InterceptorBinding
),而不是像这里这样使用EJB拦截器。虽然这可能会起作用,但EJB拦截器并不是CDI中的头等公民。谢谢,这似乎是可行的。这看起来确实是一个更优雅的解决方案!谢谢你清楚的解释@不客气。顺便说一下,我看到你把自己的答案标记为解决方案。我的回答遗漏了什么吗?只是问我是否可以/应该提供任何进一步的信息:)Hmzz似乎无法工作ip.getMember().getDeclaringClass().getName();提供MyBean.class。您没有提供任何代码片段,因此我无法知道什么是MyBean.class,您希望得到什么?还有,你的豆子的范围是什么?嗯,我明白了。事实上,这里有EJB bean,而不仅仅是CDI。我不确定我们是否合作。我会再检查一遍,然后给你打电话。谢谢,这似乎有效。这看起来确实是一个更优雅的解决方案!谢谢你清楚的解释@不客气。顺便说一下,我看到你把自己的答案标记为解决方案。我的回答遗漏了什么吗?只是问我是否可以/应该提供任何进一步的信息:)Hmzz似乎无法工作ip.getMember().getDeclaringClass().getName();提供MyBean.class。您没有提供任何代码片段,因此我无法知道什么是MyBean.class,您希望得到什么?还有,你的豆子的范围是什么?嗯,我明白了。事实上,这里有EJB bean,而不仅仅是CDI。我不确定我们是否合作。我会再检查一遍,然后给你打电话。