2011-03-02 5 views
13

@ManagedProperty 주석 (매우 Possible to inject @ManagedBean as a @ManagedProperty into @WebServlet?과 유사하지만 서블릿이 아닌 bean에 주입하고 있음)을 통해 JSF 관리 빈을 다른 관리 빈으로 주입하려고합니다. 이것은 내가 뭘하는지입니다 :@ManagedProperty annotation을 통해 전체 관리 빈을 주입하는 방법은 무엇입니까?

@ManagedBean 
public class Foo { 
    @ManagedProperty(value = "#{bar}") 
    private Bar bar; 
} 

@ManagedBean 
public class Bar { 
} 

는 작동하지 않습니다 (JSF 2.0/인 Mojarra 2.0.3) :

SEVERE: JSF will be unable to create managed bean foo when it is 
requested. The following problems where found: 
- Property bar for managed bean foo does not exist. Check that 
    appropriate getter and/or setter methods exist. 

모두 그것을 가능 아니면 통해 프로그래밍이 주입 할 필요가 FacesContext? FacesContext 종속성을 해결하고 주입 할 때

답변

29

당신은이 속성을 찾을 수 없습니다 there.otherwise 그렇게 적절한 세터/게터가 있어야한다 세터 주입을 사용 세터와 게터

@ManagedBean 
public class Foo { 
    @ManagedProperty(value = "#{bar}") 
    private Bar bar; 
    //add setters and getters for bar 
    public Bar getBar(){ 
     return this.bar; 
    } 
    public void setBar(Bar bar){ 
     this.bar = bar;; 
    } 
} 

를 추가 할 필요가

+1

xhtml JSF는 _foo를 getFoo 및 setFoo로 변환하고 관리되는 di는 실제로 get_foo 및 set_foo가 필요합니다. – Rob

+1

기타 참고 사항> 주입시에만 세터가 필요합니다. 참조> http://www.mkyong.com/jsf2/injecting-managed-beans-in-jsf-2-0/ – Sergio

관련 문제