How does JSF find beans annotated with @ManagedBean?

亡梦爱人 提交于 2019-12-29 03:31:07

问题


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


回答1:


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:

  • Structure for multiple JSF projects with shared code



回答2:


Actually, the container scans all classes within the current WAR file. Scanning all the classpath would be very long and costly.

That's why you can put your annotated managed beans within the WAR file, and not in a JAR file.

If you really need to put your classes in a JAR file in the classpath, you can use the faces-config.xml file to tell JSF which classes are managed bean.



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!