问题描述
据我所知,要使用 @Annotations(或 C# 中的 [Attributes]),您必须引用类元数据,以便您可以询问该类是否已注释(属性).
As far as I know, for using @Annotations (or [Attributes] in C#) you have to have a reference to the class metadata, so that you can ask if the class is annotated (attributed) or not.
我的问题是 JSF 实现如何找到所有使用 @ManagedBean 注释的类?它是否扫描类路径中的所有类?或者有没有办法真正查询"JVM 中的注释类?
My question is how does JSF implementation find all classes annotated with @ManagedBean? Does it scan all of the classes in the class path? Or is there a way to actually "query" the JVM for the annotated classes?
我问这个是因为当我将带注释的支持 bean 直接放入我的 web 项目中时,没有问题.但是我在 JAR 文件中定义的 bean(可跨项目重用)没有注册.我必须告诉 MyFaces 指示它查看哪些 JAR 文件吗?
I'm asking this because when I put my annotated backing beans in my web project directly, there's no problem. But the beans that I define in the JAR files (to be reusable across projects) are not registered. Is there something that I have to tell MyFaces to direct it which JAR files to look at?
此外,使用注解引入了许多不错的编程模式.我想知道我是否能以某种方式找到所有带注释的类...
Also, using annotations introduce many nice patterns of programming. I want to know if I can find all annotated classes somehow...
推荐答案
我的问题是 JSF 实现如何找到所有使用 @ManagedBean 注释的类?它是否扫描类路径中的所有类?或者有没有办法实际查询"?注释类的 JVM?
首先查看 com.sun.faces.application.annotation.AnnotationManager
in Mojarra 来源.请注意,这不是 API 的一部分,而是特定于实现的.
Start by peeking around in com.sun.faces.application.annotation.AnnotationManager
in Mojarra sources. Note that this is not part of the API, but implementation-specific.
如果您打算在自己的项目中使用此类工具,我建议您使用 Reflections 而不是在家种植它.
If you intend to use such tools for your own projects, I recommend using Reflections for this instead of homegrowing it.
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
在 Java EE 环境中,最好使用 CDI.
In a Java EE environment, better yet is to use CDI instead.
我问这个是因为当我将带注释的支持 bean 直接放入我的 Web 项目中时,没有问题.但是我在 JAR 文件中定义的 bean(可跨项目重用)没有注册.我必须告诉 MyFaces 指示它查看哪些 JAR 文件吗?
要让 JSF 从 JAR 文件加载任何带注释的托管 bean,您必须将 /META-INF/faces-config.xml
文件放入 JAR 文件中.只需一个与 JSF 2.0 兼容的 <faces-config>
声明就足以让 JSF 扫描 JAR 文件以查找任何有趣的带注释的类.如果 JAR 文件中不存在 /META-INF/faces-config.xml
文件,则 JSF 不会扫描 JAR 文件以提高加载性能.
To have JSF to load any annotated managed beans from a JAR file, you have to put a /META-INF/faces-config.xml
file in the JAR file. Just a JSF 2.0 compatible <faces-config>
declaration is sufficient to get the JSF scan the JAR file for any interesting annotated classes. If the /META-INF/faces-config.xml
file is not present in the JAR file, then JSF won't scan the JAR file to improve loading performance.
以下是与 JSF 2.0 兼容的最小 faces-config.xml
文件的外观:
Here's how a minimum JSF 2.0 compatible faces-config.xml
file look like:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
</faces-config>
将其存储在 JAR 的 META-INF
文件夹中.
Store it in the META-INF
folder of the JAR.
这在JSF 2.0 规范.
...
此算法为组装基于 JSF 的 Web 组件的开发人员提供了相当大的灵活性应用.例如,一个应用程序可能包含一个或多个自定义 UIComponent 实现,以及关联的渲染器,因此它可以在名为/WEB-INF/faces-config.xml"的应用程序资源中声明它们无需以编程方式将它们注册到 Application 实例.此外,应用程序可能会选择包含一个包含META-INF/faces-config.xml"资源的组件库(打包为 JAR 文件).此资源的存在导致组件、渲染器和其他 JSF 实现类存储在此库 JAR 文件自动注册,应用程序无需任何操作.
This algorithm provides considerable flexibility for developers that are assembling the components of a JSF-based web application. For example, an application might include one or more custom UIComponent implementations, along with associated Renderers, so it can declare them in an application resource named "/WEB-INF/faces-config.xml" with no need to programmatically register them with Application instance. In addition, the application might choose to include a component library (packaged as a JAR file) that includes a "META-INF/faces-config.xml" resource. The existence of this resource causes components, renderers, and other JSF implementation classes that are stored in this library JAR file to be automatically registered, with no action required by the application.
另见:
- 具有共享代码的多个 JSF 项目的结构
这篇关于JSF 如何找到使用@ManagedBean 注释的bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!