5

私は@Statelessという2つのインターフェース(リモートとローカル)を実装しています。また、インタフェースなしのビューでBeanにアクセスするためのアノテーションを@LocalBeanと追加しました。 EJBでWELD-001408 EntityManagerをインジェクトするときに満たされていない依存関係

@PersistenceContext(unitName="WeatherStationJPA") 
private EntityManager entityManager; 

をし、それが何の問題もなく動作します。

We use the "resource producer" pattern, from CDI, to "alias" the old fashioned @PersistenceContext injection of the entity manager to a CDI style injection. This allows us to use a consistent injection style (@Inject) throughout the application.

は今、以前に私が使用している:私はfrom this example of JBoss AS7 quickstartを取られ、この理由のため@Injectを使用

@Stateless 
@LocalBean 
public class WeatherDataBean implements WeatherDataBeanRemote, WeatherDataBeanLocal { 
    @Inject 
    private EntityManager entityManager; 

    public WeatherDataBean() { 

    } 
    // ....attributes, getter & setter methods .... 
} 

。しかし@Inject注釈、私はこのエラーを取得すると:私はエンティティマネージャを注入しようとした場合、私はこのエラーを取得することですなぜ

public class Resources { 
    @SuppressWarnings("unused") 
    @PersistenceContext(unitName="WeatherStationJPA") 
    @Produces 
    private EntityManager entityManager; 

    @Produces 
    FacesContext getFacesContext() { 
     return FacesContext.getCurrentInstance(); 
    } 
} 

:ここ

WELD-001408 Unsatisfied dependencies for type [EntityManager] with qualifiers [@Default] at injection point [[field] @Inject private ejb.WeatherDataBean.entityManager]

は、私が定義されたクラスのリソースを持っているかですか?

EDIT:@LightGuardからの要求に 私は注釈を参照するために使用していますパッケージ追加してい:

  1. をWeatherDataBeanがありますがあり

    import javax.ejb.LocalBean; 
    import javax.ejb.Stateless; 
    import javax.inject.Inject; 
    
  2. リソースを:

    import javax.enterprise.inject.Produces; 
    import javax.faces.context.FacesContext; 
    import javax.inject.Singleton; 
    import javax.persistence.EntityManager; 
    import javax.persistence.PersistenceContext; 
    
+0

すべてが正しいように見えます。正しい注釈(チェックパッケージ)をお持ちですか?また、物を梱包する方法によっても異なる場合があります。 – LightGuard

+0

私の答えを確認してくださいこれらの注釈に使用するパッケージを追加しました。 EJBプロジェクト、EJBClientプロジェクト、JPAプロジェクト、JSFプロジェクトをパッケージ化するためにEARプロジェクトを使用しています。 –

+0

ああ、あなたがEARを使用しているのであれば、ちょっと変わってしまいます。インジェクションを行う場所、クラスが入っているライブラリ、beans.xmlファイルがある場所を配置する必要があります。 – LightGuard

答えて

5

私は同じ問題を抱えていた、私は同様のエラーに遭遇してきた私のプロジェクトに

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; 
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 

/** 
* 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(); 
    } 

} 
6

を、このクラスを追加することで、これを固定し、私は豆を入れていないが判明しました。 xmlファイルをWEB-INFフォルダに保存します。 beans.xmlファイルは、WEB-INFフォルダにある空のファイルにすることができます。 JBossASはそのファイルをチェックしてCDIサービスを開始します。

関連する問題