@ManagedProperty does not work in a CDI managed bean

前端 未结 1 759
鱼传尺愫
鱼传尺愫 2021-01-14 08:59

I try to learn JSF and encountered on a problem connected with ManagedProperty. However I have tried to use it, it always failed - null exception pointer. What am I doing wr

1条回答
  •  臣服心动
    2021-01-14 09:32

    @ManagedProperty is managed bean annotaion, that can't be used with CDI. In above code, you used CDI bean i.e. @Named that is default in JSF 2.2. In this case you can't use ManagedProperty. Please read following line copied from Java EE docs of ManagedBean.

    If this annotation is present on a class that does not have the ManagedBean annotation, the implementation must take no action on this annotation.

    For details see the link:

    http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html

    So, use @Inject instead of @ManagedProperty for CDI bean.

    @Inject
    private User user;
    

    Note that a getter/setter is unnecessary here.

    0 讨论(0)
提交回复
热议问题