2014-01-15 1 views
0

CDI를 배우고, 나는 예제를 시도하고 있습니다. 실제로 주입 지점이 웹 페이지에있을 때 InjectionPoint가 주입되지 않습니다. 희망은 그것이 의미가 있습니다!CDI InjectionPoint가 웹 페이지에서 작동하지 않습니다.

webpage.xhtml :

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> 
    <h:head> 
     <title>Facelet Title</title> 
    </h:head> 
    <h:body style="background-color: #{userInfo.backgroundColor};"> 
     Hello from Facelets. 
    /h:body> 
</html> 

UserInfo.java

@Named 
public class UserInfo implements Serializable { 

    private UserInterface **ui**; 
    private static final Logger logger = Logger.getAnonymousLogger(); 

    @Inject 
    public void setUserInterface(UserInterface ui, **InjectionPoint p**) { 
     logger.log(Level.INFO, "In UserInfo {0}", p); 
     this.ui = ui; 
    } 

    public String getBackgroundColor() { 
     return ui.getBackgroundColor(); 
    } 
} 

UserInterfaceFactory.java :

public class UserInterfaceFactory { 

    private static final Logger logger = Logger.getAnonymousLogger(); 

    @Produces 
    public UserInterface getUserInterface(@New AdultUserInterface ai, **InjectionPoint p**)  { 
     logger.info("In UserInterfaceFactory " + p); 
     return ai; 
    } 
    } 

InjectionPoint

웹 페이지와 두 개의 클래스있다 변종 가능한 경우 UserInfo.java 값이 지정되지 않습니다. 그래서 출력이있다 : UserInterfaceFactory 에서

In UserInfo null 

InjectionPoint 변수는 값이 할당 가져옵니다. 출력 그래서이 :

In UserInterfaceFactory [parameter 1] of [method] @Inject public com.imacious.learncdi.client.UserInfo.setUserInterface(UserInterface, InjectionPoint) 

내 질문에 다음 IS 한 경우에 InjectionPoint '작업'을 수행, 그리고 기타의 이유. 더 나은 방법은, 왜 주사가 웹 페이지 (jsf 또는 jsp) 일 때 작동하지 않는 것입니까?

+0

빈이 웹 페이지에 삽입 될 때 InjectionPoint를 사용하는 이유는 무엇입니까? –

+0

오! 언급 했어야했다. 나는 단지 실험 중이었습니다 - 나는 InjectPoint에 대한 자세한 내용이 아니라 Weld 사용자 가이드를 읽었습니다. 나도 사양에 충분하지 않을까 두렵다. – Emmanuel

답변

0

이것은 설계대로 작동합니다.

Bean을 EL에서 참조하기 때문에 주입이 없으므로 InjectionPoint가 없습니다. 아래의 사례 2의 1.0 스펙을 참조하십시오.

이 과정 인스턴스화 종속 오브젝트 @Default 형 InjectionPoint과 규정의 각 주입 지점은 수신 용기 보장해야한다 :

  • 주입 지점을 나타내는 InjectionPoint 인스턴스되는 종속 오브젝트 것 내로

  • 어떤 주입 지점에도 주입되지 않은 경우 null 값입니다.

관련 문제