@PersistenceContext无法解析为某个类型

时间:2012-10-13 09:33:24

标签: dependency-injection annotations javabeans managed-bean

我正在运行一个代码,其中存在依赖项@PersistenceContext和字段private EntityManager em;这两个都无法解析为一个类型,这个错误的含义是什么,我该如何解决呢? 代码在这里:

package org.jboss.tools.examples.util;    

import java.util.logging.Logger;     
import javax.enterprise.context.RequestScoped;     
import javax.enterprise.inject.Produces;     
import javax.enterprise.inject.spi.InjectionPoint;     
import javax.faces.context.FacesContext;     

/**    
 * This class uses CDI to alias Java EE resources, such as the persistence context, to     
CDI beans    
 *     
 * <p>    
 * Example injection on a managed bean field:    
 * </p>    
 *     
 * <pre>    
 * &#064;Inject    
 * private EntityManager em;    
 * </pre>    
 */    
public class Resources {    
   // use @SuppressWarnings to tell IDE to ignore warnings about field not being         referenced directly    
   @SuppressWarnings("unused")    
   @Produces    
   @PersistenceContext    
   private EntityManager em;    

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

   @Produces    
   @RequestScoped    
   public FacesContext produceFacesContext() {    
      return FacesContext.getCurrentInstance();    
   }    
}    

1 个答案:

答案 0 :(得分:0)

您缺少PersistenceContext批注和EntityManager的导入语句

import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;