在 JSF 中获取 @ManagedBean 的范围
Get scope of a @ManagedBean in JSF
我目前正在开发一个项目,其中基础 class 扩展了很多 beans。
在此基础上有一个通用方法 class 基本上我需要做的唯一更改是检查当特定调用失败时调用该方法的当前实例是否是应用程序范围的 bean。如果是,我只需要记录错误消息和 return 缓存的响应;如果不是,我必须提出一个例外。
除了在扩展此基础 class 的每个应用程序作用域 bean 上使用 instanceof
之外,有什么方法可以知道对象 this
是否是应用程序作用域 bean?
您可以查看某个实例的class是否有特定的注解。例如调用:
Annotation[] annotations = this.getClass().getAnnotations();
将为您提供当前实例的所有注释。或者你可能想找出一些特定的注释:
ApplicationScoped annotation = this.getClass().getAnnotation(ApplicationScoped.class);
如果 class 没有用 @ApplicationScoped
注释,return null
在继承的情况下,方法 getAnnotations()
return 父 class 的注解也用 @RequestScoped
class 进行注解 return 也是一个 @ApplicationScoped
如果它的父级是这样声明的。另一方面,JSF 管理的 bean 将覆盖父级的范围。
希望对您有所帮助。
我目前正在开发一个项目,其中基础 class 扩展了很多 beans。
在此基础上有一个通用方法 class 基本上我需要做的唯一更改是检查当特定调用失败时调用该方法的当前实例是否是应用程序范围的 bean。如果是,我只需要记录错误消息和 return 缓存的响应;如果不是,我必须提出一个例外。
除了在扩展此基础 class 的每个应用程序作用域 bean 上使用 instanceof
之外,有什么方法可以知道对象 this
是否是应用程序作用域 bean?
您可以查看某个实例的class是否有特定的注解。例如调用:
Annotation[] annotations = this.getClass().getAnnotations();
将为您提供当前实例的所有注释。或者你可能想找出一些特定的注释:
ApplicationScoped annotation = this.getClass().getAnnotation(ApplicationScoped.class);
如果 class 没有用 @ApplicationScoped
null
在继承的情况下,方法 getAnnotations()
return 父 class 的注解也用 @RequestScoped
class 进行注解 return 也是一个 @ApplicationScoped
如果它的父级是这样声明的。另一方面,JSF 管理的 bean 将覆盖父级的范围。
希望对您有所帮助。