
上QQ阅读APP看书,第一时间看更新
TransientReference
The following is a bean marked with the @TransientReference annotation:
@SessionScoped
@Typed(Comment.class)
public class TransientOrderManager implements Comment<String>, Serializable {
private Order order;
public TransientOrderManager() {
}
@Inject
public TransientOrderManager(@TransientReference Order order) {
this.order = order;
}
@Inject
public void initialize(@TransientReference Order order) {
this.order = order;
}
@Override
public Order getOrder() {
return order;
}
}
The order is a dependent object:
@Dependent
public class Order {...}
The comment is a simple interface:
public interface Comment<T> {
Order getOrder();
}
Now inject the orderManager:
@Inject
private Comment<String> orderManager;
then call the order: orderManager.getOrder()
The order is marked as @TransientReference so, after it is created, it will be removed. A session scoped can inject a dependent bean only if there is this annotation. If we remove the annotation, we get an error because the order is not serializable.