CDIって何?

CDI の使い途

CDI による依存性の解決

Qualifier

Scope

@ApplicationScoped?(everywhere)application
@RequestScoped?(everywhere)request〜response
@SessionScoped?(web)login〜logout。serializeable にする必要あり
@ConversationScoped?(jsf)会話スコープ
@Dependent(default)@Inject する側の Object の寿命に従う
@Scopescope の基底 annotation

Basic Example

Qualifierを省略する。(暗黙の@Anyを使う)

public interface Hello {
  String sayHello(String name);
}
 
public class HelloImpl implements Hello {
  public String sayHello(String name) {
    return "Hello " + name;
  }
}
 
@Stateless
public class HelloService {
  @Inject
  private Hello hello;

  public String sayHello(String name) {
    return hello.sayHello();
  }
}

Qualifier Example

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface Login {
}
 
public interface Hello {
  String sayHello(String name);
}
 
@Login
public class HelloImpl implements Hello {
  public String sayHello(String name) {
    return "Hello " + name;
  }
}
 
@Stateless
public class HelloService {
  @Inject
  @Login
  private Hello hello;

  public String sayHello(String name) {
    return hello.sayHello();
  }
}

Conversation Scope

@Named
@ConversationScoped
public class ConversationCDI implements Serializable {
  private int counter = 0;

  @Inject
  private Conversation conversation;

  public void begin() {
    if (conversation.isTransient()) {
      conversation.begin();
    }
  }

  public void end() {
    if (conversation.isTransient()) {
      conversation.end();
    }
  }

  public void countUp() {
    counter++;
  }

  public int getCounter() {
    return counter;
  }
}

Java#Glassfish


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2015-01-11 (日) 02:20:22 (3386d)
Short-URL:
ISBN10
ISBN13
9784061426061