简体   繁体   English

Glassfish 4扫描@PostConstruct并禁用CDI

[英]Glassfish 4 scans for @PostConstruct with CDI disabled

I'm doing and upgrade from Glassfish 3.1.2.2 to Glassfish 4.1 for a set of Spring applications. 我正在做一些Spring应用程序并从Glassfish 3.1.2.2升级到Glassfish 4.1 Since I use the Spring to handle @Inject annotations, I have disabled Glassfish' CDI using this command: 由于我使用Spring来处理@Inject注释,因此我使用以下命令禁用了Glassfish的CDI:

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? 当我禁用CDI时,为什么Glassfish会验证我的@PostConstruct Why is Glassfish validating @PostConstruct on something that can not become a bean? 为什么Glassfish在不能成为bean的东西上验证@PostConstruct How can I prevent Glassfish from interferring with anything that I'm using Spring for? 如何防止Glassfish干扰我使用Spring 任何东西

Annotation @PostConstruct is a general annotation used in any dependency injection mechanism. 注释@PostConstruct是在任何依赖注入机制中使用的通用注释。 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. Javadoc明确指出,除非在拦截器中使用,否则必须将它放在一个方法上,该方法具有void返回类型并且不会抛出任何已检查的异常。

It is weird that Spring allows checked exceptions on post-construct methods, as there is not way how to handle them. Spring允许在post-construct方法上检查异常是很奇怪的,因为没有办法处理它们。 But as this requirement is only a validation and can be ignored, Spring probably ignores checked exceptions and Glassfish does not. 但由于这个要求只是一个验证而且可以忽略,因此Spring可能会忽略已检查的异常,而Glassfish则不会。 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, ...) 可能有一个不必要的Glassfish功能,它扫描并验证所有类,即使没有在CDI或任何其他机制中使用(EJB,...)

The best is to remove checked exceptions to align the code with the documentation and make it portable. 最好的方法是删除已检查的异常,以使代码与文档保持一致并使其可移植。

You can solve this issue first by adding a web.xml with metadata-complete="true". 您可以首先通过添加带有metadata-complete =“true”的web.xml来解决此问题。 Next you will want to make sure your context are in the Web root Directory /WEB-INF/. 接下来,您需要确保您的上下文位于Web根目录/ WEB-INF /中。

With this glassfish can load all @PostConstructSpring dependencies. 使用此glassfish可以加载所有@PostConstructSpring依赖项。

More of a work around in my opinion. 在我看来,更多的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM