2
私はこのようなドメインオブジェクトを持っています。 データベースからフェッチされたデータ(この例では「タイプ」)を使用して、正しいタイプのサービスをフェッチして注入する必要があります。@Configurableドメインオブジェクトプロパティを使用して、データに基づいて特定の動作を割り当てます。
私は、DBのデータを通話中に設定されていないことを意味し、この出力を得る:私は初期化ビーンにしようとすると、
entity is a bean postconstruct: PocProduct [id=null, type=null, productName=null].. attching behavior!
は、私は同じresiltを取得します。 これを設定する正しい方法は何ですか?
@Entity
@Table(name = "AAA_POC_PROD")
@Configurable(dependencyCheck = true)
@Scope("prototype")
public class PocProduct implements Serializable, InitializingBean {
/**
*
*/
private static final long serialVersionUID = 1136936011238094989L;
@Id
private String id;
private String type;
private String productName;
@Transient
private Behaviour behaviour;
@Transient
@Autowired
private BehaviourFactory behaviourFactory;
//getters and setters
@PostConstruct
public void attachBehavior() {
System.out.println("entity is a bean postconstruct: " + this + ".. attching behavior!");
//Need to call this : depends on type which is fetched from DB
// this.behaviour = behaviourFactory.getTypeBasedBehaviour(type);
}
}
JPAのバージョンによっては、@ PostConstructもSpringの代わりに呼び出されています。しかし、本当にそのような重い解決策が必要ですか?型は静的ですか? 'BehaviourFactory'はSpringによってどのように作成され、管理されていますか?異なる 'Behavior'インスタンスもSpring管理対象ですか? –
私は言っている:春の豆と永続エンティティは、そのようにマージするべきではありません。私はあなたが探しているものが '@ PostLoad'であると言うでしょう – terjekid
@M。 Deinum。Spring InitializingBean:afterPropertiesSet()もnull値を取得します。タイプは静的です。 bahviourfactoryは、オブジェクトの型に基づいて動作を返すシングルトンです。 – RCInd