java – JSF如何找到用@ManagedBean注释的bean?

栏目: Java · 发布时间: 5年前

内容简介:据我所知,对于在C#中使用@Annotations(或[Attributes]),您必须对类元数据进行引用,以便您可以询问该类是否已注释(归因).我的问题是JSF实现如何使用@ManagedBean来注释所有类?它是否扫描类路径中的所有类?还是有一种方法来实际上“查询”JVM的注释类?我问这个是因为当我把注释的支持bean直接放在我的Web项目中时,没有问题.但是我在JAR文件中定义的bean(可跨项目可重用)未注册.有没有什么我必须告诉MyFaces来指导哪个JAR文件要看?

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

我的问题是JSF实现如何使用@ManagedBean来注释所有类?它是否扫描类路径中的所有类?还是有一种方法来实际上“查询”JVM的注释类?

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

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

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?

Mojarra 年的 com.sun.faces.application.annotation.AnnotationManager 年开始.请注意,这不是API的一部分,而是实现特定的.

如果您打算为自己的项目使用这些工具,我建议您使用 Reflections 而不是自己的项目.

Set<Class<?>> classes = reflections.getTypesAnnotatedWith(SomeAnnotation.class);

在Java EE环境中,更好的是使用CDI.

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?

要使JSF从JAR文件加载任何带注释的托管bean,必须将/META-INF/faces-config.xml文件放在JAR文件中.只是一个JSF 2.0兼容<faces-config>声明足以使JSF扫描JAR文件以获取任何有趣的注释类.如果/META-INF/faces-config.xml文件不存在于JAR文件中,则JSF将不会扫描JAR文件以提高加载性能.

以下是与JSF 2.0兼容的最少的faces-config.xml文件如下所示:

<?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文件夹中.

这是按照 JSF 2.0 specification 第11.4.2章的描述.

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

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.

也可以看看:

> Structure for multiple JSF projects with shared code

http://stackoverflow.com/questions/3717376/how-does-jsf-find-beans-annotated-with-managedbean


以上所述就是小编给大家介绍的《java – JSF如何找到用@ManagedBean注释的bean?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具