如果 ManagedBean 位于 jar 库内,则 @PostConstruct 不会被 JSF 调用

发布于 2024-12-02 03:09:59 字数 621 浏览 0 评论 0原文

我正在运行,遇到以下问题。

目前,我有一些在两个 JSF 应用程序之间共享的托管 Bean。因为我不想复制并粘贴这两个中的代码(将来会更多),所以我已将此共享托管 bean 放入 JAR 库中。我关注了这个博客: http://jsflive.wordpress.com /2011/03/24/custom-component-library/

好吧,即使我将 faces-config.xml 放在 JAR/META-INF/ 中 @ManagedBean 和 @ViewScoped没用。我不明白为什么,但如果我在 faces-config.xml 中注册 beans(JAR 的,而不是 WAR 的),这个问题就会消失。

我可以接受这一点,但令我惊讶的是,JAR 库中的托管 bean 并未调用 @PostConstruct 注释。我没有收到任何错误、警告或其他信息。我认为 bean 已被加载,但它们的注释尚未被处理。

有人遇到过这个吗?

我的环境: Glassfish 3.1.1(版本 12) JSF 2.1.3

提前致谢。

I'm running with the following problem.

I have a few Managed Beans that are shared between, at this moment, two JSF applications. As I don't want to copy and paste the code in the two (more in the coming future) I've put this shared managed beans inside a JAR library. I've followed this blog: http://jsflive.wordpress.com/2011/03/24/custom-component-library/

Well, even if I put the faces-config.xml inside JAR/META-INF/ the @ManagedBean and @ViewScoped didn't work. I couldn't realise why, but if I register the beans in faces-config.xml (JAR ones, not WAR ones) this problem goes away.

I could live with this, but for my surprise the @PostConstruct annotation didn't get called for this managed beans inside JAR library. I don't get any error, warning or else. I suppose that the beans are getting loaded, but the their annotations aren't being processed.

Have anyone faced this?

My environment:
Glassfish 3.1.1 (build 12)
JSF 2.1.3

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-12-09 03:09:59

那么@PostConstruct注解还没有被扫描到。这是导致您的 @ManagedBean 注释和赞未被扫描的同一问题的结果。

导致此问题的原因有多种:

  1. 您在 Jetty/Tomcat/JBoss AS 上使用了 Mojarra 2.1.0。这是注释扫描器中一个非常具体的错误。请参阅 issue 1937

  2. 您的 /WEB-INF/faces-config.xml 文件具有 metadata-complete="true" 属性。这与 JSF 2.0 规范 中概述的第一个要求相冲突:

    <块引用>

    11.5.1 扫描类注释的要求

    • 如果 WEB-INF/faces-config.xml 文件中的 元素包含 metadata-complete 属性,其值为“true”,则实现不得对除实现本身提供的类之外的任何类执行注释扫描。否则,请继续如下。

    • 如果运行时发现应用程序配置资源中的条目与注释之间存在冲突,
      应用程序配置资源中的条目优先。

    • 必须扫描 WEB-INF/classes 中的所有类。

    • 对于应用程序 WEB-INF/lib 目录中的每个 jar,如果该 jar 包含 “META-INF/faces-config.xml”
      文件或与正则表达式“.*\.faces-config.xml”匹配的文件(即使是空文件),必须扫描该 jar 中的所有类。


  3. 您的 JAR 文件并未放置在 /WEB-INF/lib 中,而是放置在类路径中的其他位置。这与上面概述的第四个要求相冲突。

  4. 您的 web 应用程序的 /WEB-INF/faces-config.xml 和/或 JAR 的 /META-INF/faces-config.xml 不是 JSF 2.x兼容的。它不得包含 JSF 1.x 特定的 声明,而是包含 JSF 2.x 特定的声明。

    
    <面孔配置
        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”>
    
    

    JAR 的 /META-INF 中的内容允许完全为空。

当您在 Glassfish 上使用 Mojarra 2.1.3 时,在您的特定情况下,原因 1 可能会被划伤。我敢打赌这是其他原因。

Then the @PostConstruct annotation has not been scanned. This is result of the same problem which caused that your @ManagedBean annotation and likes have not been scanned.

There are several causes of this problem:

  1. You used Mojarra 2.1.0 on Jetty/Tomcat/JBoss AS. This is a very specific bug in the annotation scanner. See issue 1937.

  2. Your /WEB-INF/faces-config.xml file has a metadata-complete="true" attribute. This conflicts the 1st requirement as outlined in JSF 2.0 specification:

    11.5.1 Requirements for scanning of classes for annotations

    • If the <faces-config> element in the WEB-INF/faces-config.xml file contains metadata-complete attribute whose value is “true”, the implementation must not perform annotation scanning on any classes except for those classes provided by the implementation itself. Otherwise, continue as follows.

    • If the runtime discovers a conflict between an entry in the Application Configuration Resources and an annotation, the
      entry in the Application Configuration Resources takes precedence.

    • All classes in WEB-INF/classes must be scanned.

    • For every jar in the application's WEB-INF/lib directory, if the jar contains a “META-INF/faces-config.xml”
      file or a file that matches the regular expression “.*\.faces-config.xml” (even an empty one), all classes in that jar must be scanned.

  3. Your JAR file is not been dropped in /WEB-INF/lib, but somewhere else in the classpath. This conflicts the 4th requirement as outlined above.

  4. Your webapp's /WEB-INF/faces-config.xml and/or your JAR's /META-INF/faces-config.xml is not JSF 2.x compatible. It must not contain a JSF 1.x specific <faces-config> declaration, but a JSF 2.x specific one.

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

    The one in JAR's /META-INF is allowed to be completely empty.

Cause 1 can be scratched in your particular case as you're using Mojarra 2.1.3 on Glassfish. I'll bet it to be the other causes.

梦初启 2024-12-09 03:09:59

另请注意,构造后方法不得声明为引发任何已检查异常。来自 stderr 的消息:

Method 'public void my.app.MyBean.postConstruct() throws java.lang.Exception' marked with the 'javax.annotation.PostConstruct' annotation cannot declare any checked exceptions. This method will be ignored.

Also note that post-construct method must not be declared to throw any checked exception. Message from stderr:

Method 'public void my.app.MyBean.postConstruct() throws java.lang.Exception' marked with the 'javax.annotation.PostConstruct' annotation cannot declare any checked exceptions. This method will be ignored.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文