Glassfish 4在禁用CDI的情况下扫描@PostConstruct [英] Glassfish 4 scans for @PostConstruct with CDI disabled

查看:273
本文介绍了Glassfish 4在禁用CDI的情况下扫描@PostConstruct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 Glassfish 3.1.2.2 升级到 Glassfish 4.1 $ c> Spring 应用程序。因为我使用 Spring 来处理 @Inject 注解,所以我使用这个命令禁用了Glassfish的CDI:

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

但是,当我部署其中一个应用程序时,出现以下错误消息:

 生命周期方法[something]不能抛出检查的异常。 
相关注释信息:注释[@ javax.annotation.PostConstruct()]
注释元素[public void com.something.MyClass.something()throws
java.io.IOException] of键入[METHOD]。有关更多详细信息,请参阅server.log。

这个类是一个抽象类,在我试图部署的应用程序中没有实现,它只是在我的类路径中。



为什么Glassfish会在我禁用时验证我的 @PostConstruct CDI?为什么Glassfish在不能成为bean的东西上验证 @PostConstruct
如何防止Glassfish干涉我使用 Spring


<注解 @PostConstruct 是任何依赖注入机制中使用的常规注释。 Javadoc 明确指出,除非在拦截器内使用,它必须放在一个方法中,该方法具有 void 返回类型,并且不会引发检查异常。



奇怪的是,Spring允许在构造后方法上检查异常,因为没有办法处理它们。但由于这个要求只是一个验证,可以忽略,Spring可能会忽略检查的异常,Glassfish不会。有可能是一个不必要的Glassfish功能,它可以扫描和验证所有类,即使没有在CDI或任何其他机制中使用(EJB,...)。

b
$ b

删除已检查的例外,以便将代码与文档对齐并使其可移植。


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?

解决方案

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.

这篇关于Glassfish 4在禁用CDI的情况下扫描@PostConstruct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆