@PostConstruct was not called by JSF if ManagedBean is inside jar library

I am running the following problem.

I have several managed Beans that are currently shared between two JSF applications. Since I don't want to copy and paste the code in two (more in the future), I put this shared managed Beans into the JAR library. I followed this blog: http://jsflive.wordpress.com/2011/03/24/custom-component-library/

Well, even if I put faces-config.xml inside JAR / META-INF / @ ManagedBean and @ViewScoped did not work. I could not understand why, but if I register Beans in faces-config.xml (JAR, not WAR), this problem will disappear.

I could live with this, but to my surprise, the @PostConstruct annotation was not called for this managed Beans inside the JAR library. I do not receive any errors, warnings or not. I believe Beans are loading, but their annotations are not being processed.

Has anyone come across this?

My environment: Glassfish 3.1.1 (build 12) JSF 2.1.3

Thanks in advance.

+6
source share
2 answers

Then the @PostConstruct annotation @PostConstruct not scanned. This is the result of the same problem that @ManagedBean your @ManagedBean annotations and likes from being scanned.

There are several causes for this problem:

  • You used Mojarra 2.1.0 on Jetty / Tomcat / JBoss AS. This is a very specific error in the annotation scanner. See question 1937 .

  • Your /WEB-INF/faces-config.xml file has the metadata-complete="true" attribute. This contradicts the first requirement described in the JSF 2.0 specification :

    11.5.1 Class scan requirements for annotations

    • If the <faces-config> element in the WEB-INF/faces-config.xml file contains the metadata-complete attribute whose value is "true" , the implementation should not annotate the scan for any classes other than those classes provided by the implementation itself. Otherwise, follow these steps.

    • If the runtime detects a conflict between the entry in the application configuration resources and the annotation, the entry in the application configuration resources takes precedence.

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

    • For each jar in the WEB-INF/lib application directory, if the jar contains the "META-INF/faces-config.xml" file or file that matches the regular expression ".*\.faces-config.xml" (even empty) , all classes in this bank must be scanned.

  • Your JAR file was not deleted in /WEB-INF/lib , but somewhere else in the classpath. This contradicts the fourth requirement described above.

  • Your webapp /WEB-INF/faces-config.xml and / or your JAR /META-INF/faces-config.xml not compatible with JSF 2.x. It should not contain JSF 1.x a specific <faces-config> declaration, but a specific JSF 2.x.

     <?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 allows completely empty.

Reason 1 may be scratched in your particular case when you use Mojarra 2.1.3 on Glassfish. I will bet that these are other reasons.

+14
source

Also note that the 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. 
+8
source

Source: https://habr.com/ru/post/896149/


All Articles