-1

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?

2
  • 2
    It is no good design. Entities are abstraction of persistence object. This should not use any services.
    – Jens
    Apr 10, 2015 at 8:11
  • Why? Why must I implement service over service to do things an entity could do on its own?
    – mibutec
    Apr 10, 2015 at 9:39

1 Answer 1

0

Finally I had to overwrite the MappingMongoConverter. How that can be done is described under Set MongoDb converter programatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.