本文介绍了JBoss7.1.3:@EJB工作,而@Inject失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近用Glassfish开发了一个应用程序,并且进展非常顺利,现在使用JBoss我正在尝试做同样的事情,但是由于诸如:@EJB有效但@Inject失败等各种问题而减慢了速度.我还没有一个特别的类,我只有一个Singleton Startup类和一个我注入的简单的Stateless类,令我惊讶的是注入不起作用.这是我的课:

I've recently developed an app with Glassfish and the development goes very smoothly, now with JBoss I'm trying to do the same but is being slow down by various problems such as: @EJB works but @Inject fails. I don't have a fancy class yet, I just have a Singleton Startup class and a simple Stateless class that I inject, and to my surprise injection doesn't work. Here's my class:

package com.czetsuya.dropship;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
@Startup
public class StartupListener {
    private Logger log = LoggerFactory.getLogger(StartupListener.class);

    @EJB
    private TestService testService;

    public StartupListener() {

    }

    @PostConstruct
    private void init() {
        testService.test();
        log.debug("startup");
    }
}

服务类别:

package com.czetsuya.dropship;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

@Stateless
@LocalBean
public class TestService {
    public TestService() {

    }

    public void test() {
        System.out.println("run");
    }
}

另一件事是,如果我向记录器注入以下生产者,则它也将不起作用并引发:

The other thing is if I inject my logger with the following producer it also doesn't work and throws:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Logger] with qualifiers [@Default] at injection point [[field] @Inject private com.czetsuya.dropship.StartupListener.log]

记录器生产者:

@Produces
Logger createLogger(InjectionPoint injectionPoint) {
    return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}

请注意,我的ejb和war项目中包含beans.xml.

Note that I have beans.xml in my ejb and war project.

我的beans.xml文件:

My beans.xml file:

<beans 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/javaeehttp://java.sun.com/xml/ns/javaee/beans_1_0.xsd"></beans>

推荐答案

在我Project clean和maven clean并将bean.xml内容更改为:

Hmm my bad, seems like a corrupt build, after I Project clean and maven clean and change beans.xml content to:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="urn:java:ee" xmlns:security="urn:java:org.jboss.seam.security" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd"></beans>

现在可以进行注射.以及记录器注入.

Injection now works. As well as the logger injection.

这篇关于JBoss7.1.3:@EJB工作,而@Inject失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 05:43