我的问题与这个问题类似。我有一个BaseBean,目前只有一个属性,该属性被注释为@ManagedProperty.
但是,当我在命令按钮的操作方法中访问此继承的托管属性的 getter 时,它返回 null。我调试并确认基本 bean 构造函数被调用了两次 - 一次在页面加载时调用,下一步在单击按钮时调用,如上述链接中所述。
我遵循了文章选择的答案以及这篇文章中提到的建议,但无济于事。
以下是我的代码:
public abstract class BaseBean
{
@ManagedProperty(value = "#{serviceLocator}")
private IServiceLocator serviceLocator;
public IServiceLocator getServiceLocator() {
return serviceLocator;
}
public void setServiceLocator(IServiceLocator serviceLocator) {
this.serviceLocator = serviceLocator;
}
}
Run Code Online (Sandbox Code Playgroud)
@ManagedBean
@ViewScoped
public class RegistrationBean extends BaseBean implements Serializable
{
private static final long serialVersionUID = -6449858513581500971L;
private String userID;
private String password;
private String firstName;
private String lastName;
private String email; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个JAX-RS库(不是应用程序).
我有:
abstract class A {
@PostConstruct
private void constructed_a() {} // not invoked
@Inject
private Some some;
}
public abstract class B extends A {
@PostConstruct
private void constructed_b() {} // not invoked
}
Run Code Online (Sandbox Code Playgroud)
和测试类:
@Path("c")
public class C extends B {
@PostConstrct
private void constructed_c() {} // invoked
}
Run Code Online (Sandbox Code Playgroud)
我正在测试jersey测试框架v2.17
我发现只constructed_c()调用它并且不调用祖先定义的那些方法.请注意,在类中some声明的field()已正确注入.@InjectA
这是正常的吗?我该怎么办?
结论
我使用embedded-glassfish进行了测试,发现正如Antonin Stefanutti指出的那样,这些回调方法按预期顺序调用.
constructed_a()
constructed_b()
constructed_c()
Run Code Online (Sandbox Code Playgroud)