EJB注入不满意的依赖项

时间:2017-09-16 15:47:53

标签: ejb wildfly inject

我正在尝试使用Wildfly和数据库连接创建一个EJB Web项目来保存我的类“Article”。但是,当我启动我的应用程序时,我收到错误

  

引起:org.jboss.weld.exceptions.DeploymentException:WELD-001408:   带有限定符@Default的ArticleDAO类型的不满意依赖项   在注入点[BackedAnnotatedField] @Inject private   serrvices.Customer.dao at serrvices.Customer.dao(Customer.java:0)

此异常是由我的@Inject语句引起的,但我无法弄清楚问题出在哪里。我已经在网上搜索并在meta-inf文件夹中放置了一个beans.xml:

  

豆发现模式= “所有” >

但没有成功。以下是我的相关课程:

Customer.java

package serrvices;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import beans.CustomerManager;


@Path("customer")
public class Customer {

    @EJB
    private CustomerManager manager;


    /*
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getCustomerCount() {
        return "<h3>Customer Count: " + manager.getCustomerCount() + "</h3>";
    }
    */

    @Inject
    private ArticleDAO dao;




    @GET
    //@Consumes({"application/xml", "application/json"})
    public void insert()
    {
        ArticleDTO userr = new ArticleDTO(999,"XXX",99);
        System.out.println("insert: " + userr);

        dao.createArticle(userr.getDescription(), userr.getPrice());
    }

}

ArticleDAO

包裹服务;

import java.util.List;


public interface ArticleDAO
{
    Article insert(Article user);
    Article update(Article user);
    void delete(Article user);

    Article findById(int id);
    List<Article> findAll();

    Article createArticle(String description, long price);
}

ArticleDAOImpl

package serrvices;

import java.util.List;

import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;



class ArticleDAOImpl
    implements ArticleDAO
{
    //private final Logger LOG = Logger.getLogger(ArticleDAOImpl.class);

    @PersistenceContext
    private EntityManager em;


    /*
     * CRUD Operations
     */ 

    @Override
    public Article insert(Article article)
    {
        System.out.println("insert(" + article + ")");
        em.persist(article);
        return article;
    }

    @Override
    public Article update(Article article)
    {
        System.out.println("update(" + article + ")");
        return em.merge(article);
    }

    @Override
    public void delete(Article article)
    {
        System.out.println("delete(" + article + ")");
        em.remove(article);
    }

    @Override
    public Article findById(int id)
    {
        System.out.println("findById(" + id + ")");
        return em.find(Article.class, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Article> findAll()
    {
        System.out.println("findAll()");
        final String hql = "SELECT u FROM " + Article.class.getName() + " AS u";        
        return em.createQuery(hql).getResultList();
    }   


    /*
     * Factory methods
     */

    @Override
    public Article createArticle(String description, long price)
    {
        System.out.println("createArticle(\"" + description + "\"," + price +")");

        Article u = new Article();
        u.setDescription(description);
        u.setPrice(price);  
        insert(u);
        return u;
    }
}

1 个答案:

答案 0 :(得分:1)

当CDI容器初始化bean并注入依赖项时,它只能将对象注入容器知道的其他对象,否则它将不会执行注入。您注射的类必须为容器所知。

尝试使用ArticleDAOImpl@Named

@ApplicationScoped CLASS注释