无法正常使用(@Inject)

时间:2015-05-25 15:28:31

标签: java java-ee cdi

当我尝试使用@Inject注入我的DAO类时,在一个管理器中使它能够将gson文件提供给当前地址当我转到特定的网址时我得到这个例外。可以有人解释我的是什么是问题。我认为问题可能来自@Inject并且它无法正常工作,但我不确定。

java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: Cannot obtain a free instance.; nested exception is: 
javax.enterprise.inject.UnsatisfiedResolutionException: Api type [cinema.dao.ProjectionDAO] is not found with the qualifiers 
Qualifiers: [@javax.enterprise.inject.Default()]
for injection into Field Injection Point, field name :  projectionDAO, Bean Owner : [ProjectionManager, Name:null, WebBeans Type:DEPENDENT, API Types:[cinema.services.ProjectionManager,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]] while invoking public java.util.Collection cinema.services.ProjectionManager.getAllProjections() with params [].
org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:324)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:240)
org.apache.openejb.server.cxf.rs.CxfRsHttpListener.onMessage(CxfRsHttpListener.java:187)
org.apache.openejb.server.rest.RsServlet.service(RsServlet.java:53)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

经理代码:

package cinema.services;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import cinema.dao.ProjectionDAO;
import cinema.model.Projection;

@Stateless
@Path("projection")
public class ProjectionManager {

    @Inject
    private ProjectionDAO projectionDAO;

    @GET
    @Produces("application/json")
    public Collection<Projection> getAllProjections(){
        return projectionDAO.getAllProjections();
    }

}

这是ProjectionDAO:

package cinema.dao;

import java.util.Collection;

import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import cinema.model.Projection;
import cinema.model.User;

@Singleton
public class ProjectionDAO {

    @PersistenceContext
    private EntityManager em;


    public Collection<Projection> getAllProjections(){
        return em.createNamedQuery("getAllProjections",     Projection.class).getResultList();
    }

    public void addProjection(Projection projection){

            em.persist(projection);

    }

    public Projection findProjectionByMovieTitle(String movieTitle){
        try {
            return em.createNamedQuery("getProjectionByMovieTitle", Projection.class)
                .setParameter("movieTitle", movieTitle).getSingleResult();
        } catch (NoResultException e){
            return null;
        }
    }

    public void buyTicket(Projection projection, User user){
        Projection foundProjection =     findProjectionByMovieTitle(projection.getMovieTitle());
        if(foundProjection != null){
            user.getCurrentProjections().add(projection);
            int newFreeSpaces = foundProjection.getFreeSpaces() - 1;
            foundProjection.setFreeSpaces(newFreeSpaces);
        }
    }

}

投影是一个简单的模型,它为电影中的不同投影提供电影标题和开始时间。

1 个答案:

答案 0 :(得分:2)

您的问题并未说明重要信息,例如正在使用的版本或您的部署方式,因此我会采取疯狂的刺刀。

  1. 您未在部署中包含beans.xml文件。我不确定你是否正在部署WAR或JAR文件。
  2. 假设您有正确放置的beans.xml文件,请尝试使用@Singleton交换@ApplicationScope。这应该更准确地发现你的课程。