java - EJB 警告 : WELD-000411: . .. 考虑使用 @WithAnnotations 或具有边界的通用类型来限制事件

标签 java servlets ejb glassfish-4

这是在启动 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/

相关文章:

java - Java 程序应该听什么,才能成为一个好的 Linux 服务?

java - 从嵌套 HashMap 中复制对象

javascript - 将 Angular JS 与 JSP 和 Servlet 集成

javax.persistence.NoResultException : getSingleResult() did not retrieve any entities

java - 是否可以在 WebSphere Liberty 中查询/浏览 JNDI 树?

java - 在 Eclipse 的控制台中显示 java 错误

java - 工作流程 LockProcess AEM - 不锁定 Assets /页面

java - Tomcat 8 中的 getServletContext().getRealPath() 返回错误的路径

java - 如何使用jdbc和tomcat连接MySQL数据库

rest - jpa:合并多对多以前的记录时被删除