Glassfish 4 scans for @PostConstruct with CDI disabled

喜你入骨 提交于 2020-01-11 06:15:17

问题


I'm doing and upgrade from Glassfish 3.1.2.2 to Glassfish 4.1 for a set of Spring applications. Since I use the Spring to handle @Inject annotations, I have disabled Glassfish' CDI using this command:

asadmin set configs.config.server-config.cdi-service.enable-implicit-cdi=false

Still, when I deploy one of my applications, I get the following error message:

The lifecycle method [something] must not throw a checked exception.
Related annotation information: annotation [@javax.annotation.PostConstruct()] 
on annotated element [public void com.something.MyClass.something() throws 
java.io.IOException] of type [METHOD]. Please see server.log for more details.

The class in question is an abstract class with no implementations in the application that I'm trying to deploy, it's just something that is on my classpath.

Why is Glassfish validating my @PostConstruct when I've disabled CDI? Why is Glassfish validating @PostConstruct on something that can not become a bean? How can I prevent Glassfish from interferring with anything that I'm using Spring for?


回答1:


Annotation @PostConstruct is a general annotation used in any dependency injection mechanism. The Javadoc explicitely states that, unless used within an interceptor, it must be put on a method, which has void return type and throws no checked exceptions.

It is weird that Spring allows checked exceptions on post-construct methods, as there is not way how to handle them. But as this requirement is only a validation and can be ignored, Spring probably ignores checked exceptions and Glassfish does not. There is possibly an unnecessary Glassfish feature, that it scans and validates all classes, even if not used in CDI or any other mechanism (EJB, ...)

The best is to remove checked exceptions to align the code with the documentation and make it portable.




回答2:


You can solve this issue first by adding a web.xml with metadata-complete="true". Next you will want to make sure your context are in the Web root Directory /WEB-INF/.

With this glassfish can load all @PostConstructSpring dependencies.

More of a work around in my opinion.



来源:https://stackoverflow.com/questions/35411854/glassfish-4-scans-for-postconstruct-with-cdi-disabled

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