这是在启动 Web 应用程序之前执行一些任务的代码(我使用的是 glassfish4):
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.servlet.ServletContextEvent;
/**
*
* @author Ernestas Gruodis
*/
@Startup
@Singleton
public class ServerInit {
/**
* Do some code execution before web application starts up.
*/
@PostConstruct
public void init() {
System.out.println("Initialising");
}
/**
* Do some code execution on web application exit.
* @param sce the servlet context event.
*/
@PreDestroy
public void destroy(ServletContextEvent sce) {
}
}
但是在启动过程中我收到了这些警告:
WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
这里有什么问题吗?
最佳答案
这些警告是由 GlassFish 中的隐式 CDI 扫描引起的。通过定义 beans.xml
文件解决此问题,您可以在其中将 bean-discovery-mode
设置为 none
。例如,在 Maven 项目中将 beans.xml
放在 src/main/resources/META-INF/
下,您的 beans.xml
文件可能看起来像这样:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="none">
</beans>
这将关闭对整个应用程序的扫描,因此请确保在需要的地方正确注释。您还可以使用 asadmin
命令更改 GlassFish 的全局设置以禁用对所有应用程序的扫描:
asadmin set configs.config.server-config.cdi-service.enable-implicit-cdi=false
有关这方面的更多信息,我建议阅读 Enable and disable implicit scanning per JAR在 github 的 glassfish 问题队列中。
关于java - EJB 警告 : WELD-000411: . .. 考虑使用 @WithAnnotations 或具有边界的通用类型来限制事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33025693/