JavaEE Wildfly EJB未注入,仅用于战争项目 [英] JavaEE Wildfly EJB not injected, war-only project

查看:119
本文介绍了JavaEE Wildfly EJB未注入,仅用于战争项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaEE的新手,我使用 Hibernate 作为JPA提供程序,使用 gradle 构建并部署到 Wildfly 。我想对数据库感知类使用 @Stateless bean,它将在数据库上执行所有操作,然后将其注入到所有包含REST的 JAX-RS类中

I'm new to JavaEE and I created a "hello world" project, using Hibernate as JPA provider, build using gradle and deployed to Wildfly. I want to use @Stateless bean for database-aware class, that will do all operations on DB, and then inject it to all "JAX-RS" classes, that contain REST endpoints.

数据库类:

@Stateless
public class DatabaseManager {

    @PersistenceContext
    EntityManager entityManager;

    public DatabaseManager() {
    }

    public String sayHello() {
        // do some db-stuff
        return "EHLO";
    }
}

REST类:

@Path("/")
@SessionScoped
public class RestMainEndpoint implements Serializable {

    @EJB
    private DatabaseManager databaseManager;

    public RestMainEndpoint() {
    }

    @GET
    @Path("/hello")
    @Produces("text/plain")
    public String helloFromDb() {
        return databaseManager.sayHello();
    }
}

问题是 @EJB专用DatabaseManager databaseManager; 始终为 null

Problem is that @EJB private DatabaseManager databaseManager; is always null. It never gets injected.

我知道REST可以正常工作,因为所有与数据库无关的端点都可以正常工作。我还假定创建并初始化 DatabaseManager bean,因为我可以在Wildfly管理面板上看到它。

I know that the REST is working, since all non-db related endpoints are working fine. I also assume that DatabaseManager bean is created and initialized, since I can see it on Wildfly admin panel.

< a href = https://i.stack.imgur.com/92ebY.jpg rel = nofollow noreferrer>

我不确定我是否正确打包和部署它-它们都打包成一个 war 并部署。 我是否需要使用 ear 或创建单独的项目?我尝试过,但是无法成功将它转换为带有gradle的 ear 项目。

I'm not sure if I'm packaging and deploying it right - it's all packed to single war and deployed. Is it necessary for me to use ear or create separate project? I tried, but I was unable to successfully convert it to ear project with gradle.

编辑:在我的源文件的 WEB-INF 目录中,我没有任何jar,只有web.xml,并且最近添加了 beans.xml (似乎没有任何作用)。

edit: in WEB-INF directory of my source files I won't have any jars, only web.xml and recently added beans.xml (which doesn't seem to have any effect). Is it necessary?

web.xml 内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Desktopeo</display-name>
    <servlet>
        <servlet-name>desktopeo</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>
                jersey.config.server.provider.packages
            </param-name>
            <param-value>
                edu.desktopeo.server
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>desktopeo</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

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="all">
</beans>

但是,在输出 war 存档(实际上是一个zip存档),我有很多jar:

However, in output war archive (which is really a zip archive), I have a lot of jars:

我的项目jar: Commons-1.0。 jar (几个POJO类)

My project jars: Commons-1.0.jar (few POJO classes)

我的lib jar :(我自己使用gradle添加的lib)

My lib jars: (libs I added by myself using gradle)

commons-codec-1.10.jar
commons-collections4-4.0.jar
commons-io-2.4.jar
commons-lang3-3.4.jar
guava-19.0-rc2.jar

与数据库相关的jar :

My Database-related jars:

hibernate-commons-annotations-5.0.0.Final.jar
hibernate-core-5.0.2.Final.jar
hibernate-entitymanager-5.0.2.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
sqljdbc4.jar // JDBC driver for MS SQL

其他jar :(可能主要是其他依赖项)

Other jars: (probably mostly other dependencies)

javaee-api-7.0.jar
activation-1.1.jar
antlr-2.7.7.jar
aopalliance-repackaged-2.4.0-b31.jar
dom4j-1.6.1.jar
genson-1.3.jar
geronimo-jta_1.1_spec-1.1.1.jar
hk2-api-2.4.0-b31.jar
hk2-locator-2.4.0-b31.jar
hk2-utils-2.4.0-b31.jar
jandex-1.2.2.Final.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-1.jar
javax.inject-2.4.0-b31.jar
javax.mail-1.5.0.jar
javax.ws.rs-api-2.0.1.jar
jboss-logging-3.3.0.Final.jar
jersey-client-2.22.1.jar
jersey-common-2.22.1.jar
jersey-container-servlet-2.22.1.jar
jersey-container-servlet-core-2.22.1.jar
jersey-guava-2.22.1.jar
jersey-media-jaxb-2.22.1.jar
jersey-server-2.22.1.jar
jsoup-1.8.3.jar
log4j-1.2.17.jar
slf4j-api-1.7.2.jar
slf4j-log4j12-1.7.2.jar
osgi-resource-locator-1.0.1.jar
validation-api-1.1.0.Final.jar
xml-apis-1.0.b2.jar






已解决的问题



正如 Steve C 在回答中所写,问题与不必要的 jar 的输出 war 。我将其中几个的范围更改为提供的,它有所帮助(并且使 war 缩小了四倍)。


PROBLEM SOLVED

As Steve C wrote in the answer, the problem was related to unnecessary jars in output war. I changed scope of few of them to provided and it helped (and also made war 4 times smaller).

服务器端gradle版本,以供将来参考:

Server side gradle build, for future reference:

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        jcenter()
    }
}

configurations {
    provided
}

sourceSets {
    main.compileClasspath += configurations.provided
    test.compileClasspath += configurations.provided
    test.runtimeClasspath += configurations.provided
}

idea {
    module {
        scopes.PROVIDED.plus += [configurations.provided]
    }
}

dependencies {
    provided 'javax:javaee-api:7.0'
    provided 'org.hibernate:hibernate-core:5.0.2.Final'
    provided 'org.hibernate:hibernate-entitymanager:5.0.2.Final'

    // other libs, with regular "compile"
}


推荐答案

由于您已将自己的JAX-RS实现副本打包到Web应用程序中,因此没有注入EJB。此实现不知道如何注入EJB。

Your EJB is not being injected because you have packaged your own copy of a JAX-RS implementation into your web app. This implementation does not know how to inject EJBs.

由于这是WildFly 9,因此您可以摆脱所有球衣罐,它们的依赖项以及完整的web.xml。

As this is WildFly 9, you can get rid of all the jersey jars, their dependencies and the complete web.xml.

在gradle文件中,您需要确保为javaee-api-7.0依赖项提供的范围是提供的 。由于服务器提供了这些类,因此不应将其与您的应用程序打包在一起。您还需要完全删除对以下内容的gradle引用:

In your gradle file you need to ensure that the javaee-api-7.0 dependency is given a scope of provided. It should not be packaged with your application as the server provides these classes. You also need to completely remove gradle references to:


  • activation-1.1.jar

  • geronimo- jta_1.1_spec-1.1.1.jar

  • javax.annotation-api-1.2.jar

  • java.inject-1.jar

  • javax.inject-2.4.0-b31.jar

  • javax.mail-1.5.0.jar

  • javax.ws.rs-api-2.0.1.jar

  • validation-api-1.1.0.Final.jar

  • xml-apis- 1.0.b2.jar

  • activation-1.1.jar
  • geronimo-jta_1.1_spec-1.1.1.jar
  • javax.annotation-api-1.2.jar
  • java.inject-1.jar
  • javax.inject-2.4.0-b31.jar
  • javax.mail-1.5.0.jar
  • javax.ws.rs-api-2.0.1.jar
  • validation-api-1.1.0.Final.jar
  • xml-apis-1.0.b2.jar

因为这些类都是由javaee-api-7.0依赖项提供的。

because these classes are all provided by the javaee-api-7.0 dependency.

最后,添加一个包含以下内容的Java源文件:

Finally, add a java source file containing:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
}

它提供了用web.xml删除的配置,您应该可以使用。

which provides the configuration removed with the web.xml and you should be good to go.

这篇关于JavaEE Wildfly EJB未注入,仅用于战争项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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