如何通过@ManagedProperty 注释注入整个托管 bean?

编程入门 行业动态 更新时间:2024-10-24 04:42:31
本文介绍了如何通过@ManagedProperty 注释注入整个托管 bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 @ManagedProperty 注释(非常类似于 是否可以将@ManagedBean 作为@ManagedProperty 注入@WebServlet?,但我注入的是bean,而不是servlet).这就是我正在做的:

I'm trying to inject entire JSF managed bean into another managed bean by means of @ManagedProperty annotation (very similar to Possible to inject @ManagedBean as a @ManagedProperty into @WebServlet?, but I'm injecting into a bean, not a servlet). This is what I'm doing:

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

@ManagedBean
public class Bar {
}

不起作用(JSF 2.0/Mojarra 2.0.3):

Doesn't work (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 以编程方式进行注入?

Is it possible at all or I need to do this injection programmatically via FacesContext?

推荐答案

需要添加setter和getter

You need to add setters and getters

@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;;
  }
}

FacesContext 将解析并注入依赖项时,它将使用 setter 注入,因此应该存在适当的 setter/getter.否则它将找不到属性

When the FacesContext will resolve and inject dependencies it will use setters injection so appropriate setters/getters should be there.otherwise it won't find the property

这篇关于如何通过@ManagedProperty 注释注入整个托管 bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 16:11:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/808787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   ManagedProperty   bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!