vendredi 10 avril 2015

Wiring entities loaded via repository

Inside my entities managed by spring data I need some services that should be autowired by spring.



public class MyEntity {
@Autowired
private SomeService service;

@Id
private String id;

...
}


Is it possible to tell spring to autowire the given service when loading that entity?


I know I could do something like this:



public class Worker {
@Autowired
private AutowireCapableBeanFactory autowireBeanFactory;

@Autowired
private MyEntityRepository repo;

public void doSomething() {
MyEntity entity = repo.findOne("1");
autowireBeanFactory.autowireBean(entity);
entity.useService();
}
}


Can I automate that autowiring?


Aucun commentaire:

Enregistrer un commentaire