简体   繁体   English

从JBoss AS 7降级到JBoss AS 5时Weld Servlet不起作用

[英]Weld Servlet not working when downgrading from JBoss AS 7 to JBoss AS 5

I have an app that runs fine on jboss7, it uses jee6 apis: jax-rs and CDI. 我有一个可以在jboss7上正常运行的应用程序,它使用jee6 api:jax-rs和CDI。 Due to production limitations I was asked to adapt it to run on Jboss 5.1.0. 由于生产限制,我被要求对其进行修改以使其在Jboss 5.1.0上运行。 To make things easy I created a minimal application, with only one service and one injection. 为了使事情变得容易,我创建了一个最小的应用程序,仅提供一项服务和一次注入。 It works fine on jb7, and I had to add the libs that are not provided for the jboss5 version. 它在jb7上工作正常,我不得不添加jboss5版本未提供的库。

jax-rs worked fine, I added RestEasy and declared the RestEasy servlet on the web.xml. jax-rs工作正常,我添加了RestEasy并在web.xml上声明了RestEasy servlet。

For CDI I included Weld, to work on servlet mode, since the application does not have servlets. 对于CDI,我包括Weld,以便在servlet模式下工作,因为该应用程序没有servlet。 Following the docs, I included the listener on the web.xml, and then found on a jboss forum that another listener is necessary to register the BeanContext to the jboss jndi. 在文档之后,我将侦听器包含在web.xml中,然后在jboss论坛上发现需要另一个侦听器才能将BeanContext注册到jboss jndi。

So what I have is: 所以我有:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <listener>
        <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
    </listener> 

    <listener>
        <listener-class>org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener</listener-class>
    </listener> 

    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>br.com.bvmf.services.ApplicationConfig</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>    

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>  
</web-app>

For the build I am using Gadle, but just to have a better understanding for not I am inserting most of the jars manually for now: 对于构建,我使用Gadle,但是为了更好地理解,我暂时不手动插入大多数jar:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'

webAppDirName = 'WebContent'

repositories {
    mavenCentral()
    flatDir name:'EasyRestJars',dirs:'libs'
}


dependencies {
    compile(

        [name : 'weld-servlet', version : '2.0.0.SP1-jboss5'],

        [name : 'jaxrs-api', version : '2.3.6.Final'],
        [name : 'jackson-core-asl', version : '1.9.9'],
        [name : 'jackson-jaxrs', version : '1.9.9'],
        [name : 'jackson-mapper-asl', version : '1.9.9'],
        [name : 'resteasy-jackson-provider', version : '2.3.6.Final'],
        [name : 'resteasy-jaxb-provider', version : '2.3.6.Final'],
        [name : 'resteasy-jaxrs', version : '2.3.6.Final'],
        [name : 'scannotation', version : '1.0.3']
    )

    providedCompile(
        [group: 'org.slf4j', name: 'slf4j-api', version: '1.5.6'],
        [group: 'javaee',    name: 'javaee-api', version: '5' ]
    )

    testCompile group: 'junit', name: 'junit', version: '4.+'

}


test {
    systemProperties 'property': 'value'
}

war{
    exclude 'WEB-INF/lib/*'
}

Now the three classes: 现在这三个类:

Tha application config just returns the service: 应用程序配置仅返回服务:

@ApplicationPath("min")
public class ApplicationConfig extends Application {

     @Override
        public Set<Class<?>> getClasses() {
         Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
            resources.add(br.com.bvmf.services.HelloWorldResource.class);
            return resources;
        }

}

The bean that should be injected: 应该注入的bean:

public class HelloProvider {    
    public String getString() {     
        return "Injected Hello";
    }
}

And the service itself: 服务本身:

@Path("/helloworld")
public class HelloWorldResource {

    @Inject
    private HelloProvider provider;

    @GET
    public String getMessage() {
        //This is where I get a null pointer
        return provider.getString();

    }
}

So the bean is not injected, just that. 因此,不会注入bean,仅此而已。 During the deploy, the log reads: 在部署期间,日志显示为:

2013-06-04 15:24:31,371 INFO  [org.jboss.webbeans.bootstrap.WebBeansBootstrap] (HDScanner) Web Beans 1.0.0.PREVIEW1
2013-06-04 15:24:32,898 INFO  [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (HDScanner) deploy, ctxPath=/min-cil-1.0-SNAPSHOT
2013-06-04 15:24:33,417 INFO  [org.jboss.weld.Version] (HDScanner) WELD-000900 2.0.0 (SP1)
2013-06-04 15:24:33,774 INFO  [org.jboss.weld.Bootstrap] (HDScanner) WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
2013-06-04 15:24:34,485 WARN  [org.jboss.weld.Bootstrap] (HDScanner) Legacy deployment metadata provided by the integrator. Certain functionality will not be available.
2013-06-04 15:24:34,809 INFO  [org.jboss.weld.environment.tomcat.Tomcat6Container] (HDScanner) Tomcat 6 detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported
2013-06-04 15:24:35,750 INFO  [org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener] (HDScanner) BeanManager reference bound to java:comp/env/BeanManager
2013-06-04 15:26:13,648 INFO  [org.jboss.resteasy.spi.ResteasyDeployment] (http-127.0.0.1-8080-2) Deploying javax.ws.rs.core.Application: class br.com.bvmf.services.ApplicationConfig

There is a note in the weld docs saying that: "Additionally, Weld Servlet supports JBoss EAP 5.1, to do this use the jboss5 variant of Weld Servlet." 焊接文档中有一条注释说:“此外,Weld Servlet支持JBoss EAP 5.1,为此可以使用Weld Servlet的jboss5变体。” So I found this version, but it made no difference (besides more problems with slf4j). 所以我找到了这个版本,但是没有什么区别(除了slf4j还有更多问题)。

I don't think there is any integration with JAX-RS in Weld Servlet - this is only available in Java EE 6. You could look at the way it is done in JBoss AS 6 and try to backport it perhaps? 我认为Weld Servlet中没有与JAX-RS集成-仅在Java EE 6中可用。您可以看看它在JBoss AS 6中的完成方式,然后尝试反向移植吗? Or look at the CDI and RESTEasy docs to work out how to add it (basically, you need to register some sort of post-construct listener with RESTEasy, and then use CDI APIs to inject the instance). 或查看CDI和RESTEasy文档以了解如何添加它(基本上,您需要在RESTEasy中注册某种构造后侦听器,然后使用CDI API注入实例)。

It seems Weld is not supported on JBoss 5.1 afterall. 看来JBoss 5.1毕竟不支持Weld。 In the end I changed the dependency injection to Spring with annotations and autowire. 最后,我使用注解和自动装配将依赖项注入更改为Spring。 Spring played well with RESTEasy, so its all fine now. Spring在RESTEasy上玩的很好,所以现在一切都很好。

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

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