如何JSF找到@ManagedBean注解的bean? [英] How does JSF find beans annotated with @ManagedBean?
问题描述
据我所知,使用@Annotations(或C#[属性]),你必须有对类的元数据的引用,这样就可以问,如果类被注释(归属),或没有。
我的问题是如何JSF实现找到@ManagedBean注释的所有类?是否扫描所有的类在类路径?或者是有办法实际上是查询的JVM注释类?
我问这是因为当我把我的注解支持bean在我的web项目中直接,没有任何问题。但是,我在JAR文件中定义的豆(要跨项目重复使用的)没有注册。是否有我要告诉MyFaces的指示它在看哪些JAR文件?什么
此外,使用说明介绍编程的许多漂亮的图案。我想知道,如果我能以某种方式找到所有注解的类...
的我的问题是如何JSF实现找到@ManagedBean注释的所有类?是否扫描所有的类在类路径?或者是有办法实际上是查询的JVM注释类?的
块引用>在<偷看一开始左右href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/org.glassfish/javax.faces/2.2.0/com/sun/faces/application/annotation/AnnotationManager.java#AnnotationManager\"相对=nofollow>
com.sun.faces.application.annotation.AnnotationManager
中的 Mojarra 来源。注意,这不是API的一部分,但具体的实施-如果您打算使用这些工具为自己的项目,我建议使用 的思考这个,而不是homegrowing吧。
设置&LT;班级&LT;&GT;&GT;类= reflections.getTypesAnnotatedWith(SomeAnnotation.class);
在Java EE环境中,更好的是使用CDI代替。
的我问这是因为当我把我的注解支持bean在我的web项目中直接,没有任何问题。但是,我在JAR文件中定义的豆(要跨项目重复使用的)没有注册。是否有我要告诉MyFaces的指示它在看哪些JAR文件?什么的
块引用>要具有JSF从一个JAR文件中加载任何注解管理的bean,你必须把
/META-INF/faces-config.xml
文件中的JAR文件。就在JSF 2.0兼容&LT;面临-配置&GT;
声明就足以获得JSF扫描的JAR文件什么有趣的注释类。如果/META-INF/faces-config.xml
文件不在JAR文件present,那么JSF不会扫描JAR文件来提高加载性能下面是一个最小的JSF 2.0兼容
如何faces-config.xml中
文件如下:&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;面孔,配置
的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&GT;
&LT; /面孔,配置&GT;在JAR的
META-INF
文件夹存放。
11.4.2应用程序启动行为
...
此算法提供很大的灵活性对于那些组装一个基于JSF的Web的组件,开发人员
应用。例如,一个应用程序可能包括一个或多个自定义UIComponent实现方式中,随着
相关的渲染器,因此它可以在一个名为/WEB-INF/faces-config.xml的应用程序资源声明它们
无需编程与应用实例注册它们。的此外,应用程序可能选择
包括,包括META-INF / faces-config.xml中资源的一个组件库(打包为一个JAR文件)。的
此资源的存在使得组件,渲染,和存储在这个其他JSF实现类
库JAR文件将自动注册,没有应用程序所需的操作。
块引用>参见:
- Structure为共享code
多个JSF项目中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屋!