Java EE CDI, 종속성 삽입 및 특히 @Produces
을 배우고 있습니다. 내가 getGreedingCard()
메서드에서 왜 @Produces
주석이 필요한지 궁금합니다. GreetingCardImpl
과 AnotherGreetingCardImpl
두 클래스가 이미 공간으로 가져 왔기 때문에 주석이 전혀 필요하지 않습니다. 이는 정규 패키지/클래스 종속성과 동일하며 간단한 가져 오기로 문제가 해결됩니다. @producer
주석을 사용하여 종속성 주입이 필요한 이유는 무엇입니까?Java EE의 @Producer 주석
미리 감사드립니다.
public interface GreetingCard {
void sayHello();
}
public class GreetingCardImpl implements GreetingCard {
public void sayHello() {
System.out.println("Hello!!!");
}
}
public class AnotherGreetingCardImpl implements GreetingCard {
public void sayHello() {
System.out.println("Have a nice day!!!");
}
}
import com.javacodegeeks.snippets.enterprise.cdibeans.impl.AnotherGreetingCardImpl;
import com.javacodegeeks.snippets.enterprise.cdibeans.impl.GreetingCardImpl;
@SessionScoped
public class GreetingCardFactory implements Serializable {
private GreetingType greetingType;
@Produces
public GreetingCard getGreetingCard() {
switch (greetingType) {
case HELLO:
return new GreetingCardImpl();
case ANOTHER_HI:
return new AnotherGreetingCardImpl();
default:
return new GreetingCardImpl();
}
}
}
테스트 해 보셨습니까? GreetingCard는 3 개의 장소 (2 개의 클래스와 프로듀서 메소드)로 작성 될 수 있으므로, AmbiguousResolutionException을 가져야합니다. 관련 링크에 대한 링크 http://stackoverflow.com/q/22982422/3701228 – Gas