JSF 如何找到用@ManagedBean 注释的 bean? [英] How does JSF find beans annotated with @ManagedBean?

查看:29
本文介绍了JSF 如何找到用@ManagedBean 注释的 bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,要使用@Annotations(或 C# 中的 [Attributes]),您必须引用类元数据,以便您可以询问该类是否被注释(归因).

我的问题是 JSF 实现如何找到所有用 @ManagedBean 注释的类?它是否扫描类路径中的所有类?或者有没有办法实际查询"JVM 中的注释类?

我之所以这么问是因为当我将带注释的支持 bean 直接放入我的 Web 项目时,没有问题.但是我在 JAR 文件中定义的 bean(可跨项目重用)没有注册.有什么我必须告诉 MyFaces 来指导它查看哪些 JAR 文件的事情吗?

此外,使用注解引入了许多不错的编程模式.我想知道我是否能以某种方式找到所有带注释的类...

解决方案

我的问题是 JSF 实现如何找到所有用 @ManagedBean 注释的类?它是否扫描类路径中的所有类?或者有没有办法真正查询"?注释类的 JVM?

首先查看 com.sun.faces.application.annotation.AnnotationManagerMojarra 来源.请注意,这不是 API 的一部分,而是特定于实现的.

如果您打算在自己的项目中使用此类工具,我建议为此使用 Reflections 而不是在家种植它.

Set>类 = 反射.getTypesAnnotatedWith(SomeAnnotation.class);

在 Java EE 环境中,最好改用 CDI.


<块引用>

我问这个是因为当我将带注释的支持 bean 直接放入我的 Web 项目时,没有问题.但是我在 JAR 文件中定义的 bean(可跨项目重用)没有注册.有什么我必须告诉 MyFaces 来指导它查看哪些 JAR 文件的吗?

要让 JSF 从 JAR 文件加载任何带注释的托管 bean,您必须在 JAR 文件中放置一个 /META-INF/faces-config.xml 文件.只需一个与 JSF 2.0 兼容的 声明就足以让 JSF 扫描 JAR 文件以查找任何有趣的带注释的类.如果 JAR 文件中不存在 /META-INF/faces-config.xml 文件,则 JSF 将不会扫描 JAR 文件以提高加载性能.

以下是最小 JSF 2.0 兼容 faces-config.xml 文件的样子:

<人脸配置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"版本=2.0"></faces-config>

将其存储在 JAR 的 META-INF 文件夹中.

这是在 JSF 2.0 规范的第 11.4.2 章中描述的方式.

<块引用>

11.4.2 应用程序启动行为

...

该算法为正在组装基于 JSF 的 Web 组件的开发人员提供了相当大的灵活性应用.例如,一个应用程序可能包含一个或多个自定义 UIComponent 实现,以及关联的渲染器,因此它可以在名为/WEB-INF/faces-config.xml"的应用程序资源中声明它们无需以编程方式将它们注册到 Application 实例.此外,应用程序可能会选择包含一个包含META-INF/faces-config.xml"资源的组件库(打包为 JAR 文件).此资源的存在导致组件、渲染器和其他 JSF 实现类存储在此自动注册库 JAR 文件,应用程序无需执行任何操作.

另见:

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.

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?

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...

解决方案

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?

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.

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);

In a Java EE environment, better yet is to use CDI instead.


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?

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.

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>

Store it in the META-INF folder of the JAR.

This is by the way described in chapter 11.4.2 of JSF 2.0 specification.

11.4.2 Application Startup Behavior

...

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.

See also:

这篇关于JSF 如何找到用@ManagedBean 注释的 bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆