如何配置Weld / Jetty以识别@Inject注释?

时间:2018-08-02 21:39:37

标签: cdi resteasy embedded-jetty weld jboss-weld

我有一个带有Weld + RESTEasy的Jetty嵌入式项目。我配置Weld添加依赖项:

<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet</artifactId>
    <version>${my.weld.version}</version>
</dependency>

<!-- Compability library of RESTEasy for CDI -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-cdi</artifactId>
    <version>${my.resteasy.version}</version>
</dependency>

在创建Server对象时,我添加了Weld的侦听器(如Weld Docs-https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_embedded_jetty中所述):

server = new Server(PORT);
final ServletContextHandler context = new ServletContextHandler(server, CONTEXT_ROOT);
context.addEventListener(new Listener());

但是,当我尝试在资源中应用@Inject时:

@Path("/foo")
@RequestScoped
public class AcademicBean {
    @Inject
    private FooClass fooObj;
    (...)

    @POST
    @Path("/foo-action")
    @Produces("application/json;charset=UTF-8")
    public Result fooAction(List<String> paramPost) {
        fooObj.fooMethod();
    }

我在fooObj上收到了NullPointerException。你知道我是否忘记了配置吗?谢谢!

0 个答案:

没有答案