本文介绍了如果ManagedBean在jar库中,则JSF不会调用@PostConstruct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题.

目前,我有一些Managed Bean,它们在两个JSF应用程序之间共享.由于我不想将代码复制和粘贴到两者中(在未来的将来会有更多内容),因此将此共享的托管bean放在JAR库中.我关注了此博客: http://jsflive.wordpress.com /2011/03/24/custom-component-library/

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/

好吧,即使我将faces-config.xml放在JAR/META-INF/中,@ ManagedBean和@ViewScoped也不起作用.我不知道为什么,但是如果我在faces-config.xml(JAR的,而不是WAR的)中注册bean,这个问题就解决了.

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.

我可以忍受这个,但是令我惊讶的是,JAR库中的此托管bean并未调用@PostConstruct批注.我没有收到任何错误,警告或其他信息.我想这些bean正在装入,但是它们的注释未得到处理.

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?

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

My environment:Glassfish 3.1.1 (build 12)JSF 2.1.3

谢谢.

推荐答案

然后未扫描@PostConstruct批注.这是同一问题的结果,该问题导致您的@ManagedBean批注和喜欢没有被扫描.

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.

此问题有多种原因:

  1. 您在Jetty/Tomcat/JBoss AS上使用了Mojarra 2.1.0.这是注释扫描器中一个非常特定的错误.参见第1937期.

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

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:

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

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.

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

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

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.

  • 您的JAR文件未在/WEB-INF/lib中删除,但在类路径中的其他位置删除了.这与上面概述的第四个要求相冲突.

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

    您的Web应用程序的/WEB-INF/faces-config.xml和/或您的JAR的/META-INF/faces-config.xml与JSF 2.x不兼容.它不能包含特定于JSF 1.x的<faces-config>声明,而不能包含特定于JSF 2.x的声明.

    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>
    

    JAR的/META-INF中的一个可以完全为空.

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

    当您在Glassfish上使用Mojarra 2.1.3时,在特定情况下可能会刮擦原因1.我敢打赌这是其他原因.

    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.

    这篇关于如果ManagedBean在jar库中,则JSF不会调用@PostConstruct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 07-29 21:08