<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="annotated"> </beans>
public class UserMgr { @Inject @Login private User user; public void changePassword(String oldPassword, String newPassword) { if ((user.getPassword()).equals(oldPassword) { user.setPassword(newPassword); } else { throw new UpdateFailException("old password is incorrect"); } } }
public class HelloHandler { @Inject public HelloHandler() { ... } public HelloHandler(String lang) { ... } }
public class SomeBeanFactory { @Produces public static SomeBean createSomeBean() { SomeBean b = new SomeBean(); b.set(...) ... } }
@Stateless public class HelloService { @Inject public String sayHello(@Default String name) { return hello.sayHello(); } }
@Stateless public class HelloService { @Inject private Hello hello; public String sayHello(String name) { return hello.sayHello(); } }
@Stateless public class HelloService { public String sayHello(@Inject String name) { return hello.sayHello(); } }
public class Session { @Inject @New(SomeBeanImpl2.class) private SomeBean bean; ... } public class SomeBeanImpl1 implements SomeBean { ... } public class SomeBeanImpl2 implements SomeBean { ... }
public class Session { @Inject @Named("bean1") private SomeBean bean; ... } @Named("bean1") public class SomeBeanImpl1 implements SomeBean { ... } @Named("bean2") public class SomeBeanImpl2 implements SomeBean { ... }
@Qualifier @Retention(RUNTIME) @Target({METHOD, FIELD, PARAMETER, TYPE}) public @interface Login { }
@ApplicationScoped? | (everywhere) | application |
@RequestScoped? | (everywhere) | request〜response |
@SessionScoped? | (web) | login〜logout。serializeable にする必要あり |
@ConversationScoped? | (jsf) | 会話スコープ |
@Dependent | (default) | @Inject する側の Object の寿命に従う |
@Scope | scope の基底 annotation |
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 @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(); } }
@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; } }