1
votes

I am trying to create an EJB Web Project with Wildfly and a Database connection to persist my class "Article". But when I start my application I get the error

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ArticleDAO with qualifiers @Default at injection point [BackedAnnotatedField] @Inject private serrvices.Customer.dao at serrvices.Customer.dao(Customer.java:0)

This exception is caused by my @Inject statement but I can not figure out where is the problem. I searched already the web and placed a beans.xml in the meta-inf folder with:

bean-discovery-mode="all">

but no success. Here are my related classes:

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

package serrvices;

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
I know, the scanning should not make this necessary, but did you already try to annotate the beans appropriately. ArticleDAOImpl you could annotate as @ApplicationScoped. The next option would be to look, what happens if you inject ArticleDAOImpl instead of the interface. I know this should not be necessary, but perhaps some pointers could bring you further to the cause of the problems.aschoerk
put the beans.xml in WEB-INF instead of META-INF if you are in a war project. You do not need to add @ApplicationScoped annotation if discovery mode is all.Rouliboy

1 Answers

1
votes

When the CDI container initializes beans and injects dependencies, it can only inject objects into other objects that the container is aware of, otherwise it will not do the injection. The class in which you are injecting must be known to the container.

Try annotating your ArticleDAOImpl CLASS with @Named and @ApplicationScoped