1
我想用@PostLoad
中的數據庫加載其他數據來豐富實體。用@PostLoad標記的在Hibernate管理實體方法中訪問Spring beans
如何訪問@PostLoad
方法中的Spring managed beans?
我用靜態訪問醜陋的解決方案:
@Service
public class StaticApplicationContext implements ApplicationContextAware {
private static ApplicationContext ctx = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ctx = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return ctx;
}
}
@Entity
public class Car {
@Id
private Long id;
...
@Transient
private List<XType> details;
@PostLoad
private void onLoad() {
XTypeRepository repo = StaticApplicationContext.getCtx()
.getBean(XTypeRepository.class) ;
this.details = repo.findByCarId(this.id);
}
}
在
Accessing spring beans in static method
描述static
訪問
通訊的想法是有更地道的解決方案/框架糖嗎?