我想創建一個EJB Web項目與蜻蜓和數據庫連接來堅持我的類「文章」。但是,當我開始我的應用程序得到錯誤EJB注入不滿意的依賴關係
產生的原因:org.jboss.weld.exceptions.DeploymentException:WELD-001408: 與於注射點預選賽@default 型ArticleDAO不滿意的依賴[BackedAnnotatedField] @Inject在serrvices.Customer.dao(Customer.java:0)私人 serrvices.Customer.dao
此異常由我@Inject聲明引起的,但我不能找出問題出在哪裏。我搜索已經在網絡和一個放置在beans.xml中的META-INF文件夾:
豆發現模式= 「所有」>
,但沒有成功。這裏是我的相關類:
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
包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;
}
}
我知道你的註釋CLASS
ArticleDAOImpl
,掃描不應該使這個必要的,但你已經嘗試適當地註釋豆。 ArticleDAOImpl您可以註釋爲@ApplicationScoped。下一個選項是查看,如果注入ArticleDAOImpl而不是界面會發生什麼情況。我知道這不應該是必要的,但也許一些指針可能會讓你進一步解決問題的原因。 – aschoerk如果您處於戰爭項目中,請將beans.xml放入WEB-INF而不是META-INF。如果發現模式是全部,則不需要添加@ApplicationScoped註釋。 – Rouliboy