@ManagedProperty in a Spring managed bean is null

前端 未结 1 1155
醉梦人生
醉梦人生 2020-12-03 12:42

I\'ve some trouble with injecting one managedbean in another by defining a managedproperty. I\'m googling and stackoverflowing now for 3 days, but with no result...

相关标签:
1条回答
  • 2020-12-03 13:28

    Your JSF backing beans (MainBean and UserBean) should be managed either by JSF or by Spring, but not by both of them.

    If your beans are managed by JSF:

    • You annotate them with @ManagedBean and @...Scoped
    • You don't need to declare them in applicationContext.xml
    • You use @ManagedProperty instead of @Autowired, even if you need to inject beans managed by Spring (don't forget setters, @ManagedProperty requires it):

      @ManagedProperty("#{userDao}")
      private UserDao userDao;
      

    If your beans are managed by Spring:

    • You declare them in applicationContext.xml with appropriate scopes (view scope is not supported)
    • You don't need @ManagedBean and @...Scoped
    • You use @Autowired instead of @ManagedProperty and you cannot inject beans managed by JSF this way

    In both cases you need to configure Spring-JSF bridge in faces-context.xml:

    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
    
    0 讨论(0)
提交回复
热议问题